[clang] [clang][AST] Fix crash on labeled break/continue within switch condition statement expression (PR #226754)

Ambrose Leeb via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 28 08:50:36 PDT 2026


================
@@ -1535,7 +1535,9 @@ const Stmt *LabelStmt::getInnermostLabeledStmt() const {
 const Stmt *LoopControlStmt::getNamedLoopOrSwitch() const {
   if (!hasLabelTarget())
     return nullptr;
-  return getLabelDecl()->getStmt()->getInnermostLabeledStmt();
+
+  LabelStmt *Label = getLabelDecl()->getStmt();
+  return Label ? Label->getInnermostLabeledStmt() : nullptr;
----------------
Sirraide wrote:

Actually, one potential issue here is that we might end up treating a labeled break/continue as an unlabeled break/continue if this is called some time before the substatement of the `LabelDecl` is set. I wonder if is possible though. If it is, we’d probably have to move the `hasLabelTarget()` check out of this function, and then every caller would have to be changed to do the following:

1. Call `hasLabelTarget()` to see if there is a label.
2. If yes, try to get the labelled statement.
3. If we can’t, issue an error or (e.g. in codegen) assert that this didn’t return null, provided that that’s a problem depending on what we’re trying to do w/ it.

That sounds like an annoying refactor though, so I’d prefer to avoid that if possible.

https://github.com/llvm/llvm-project/pull/226754


More information about the cfe-commits mailing list