Skip to content

Conversation

@shafik
Copy link
Collaborator

@shafik shafik commented Aug 7, 2025

Static analysis complained that:

child_range(&Init, &Init+1);

in the children member function was potentially out of bounds. This is false b/c it is forming an iterator range but it would be invalid if Init was a nullptr.

I add an assertion in the constructor for this and remove to FIXME checks that are related to this. I checked the various usages and we always valid the argument is not nullptr.

Static analysis complained that:

  child_range(&Init, &Init+1);

in the children member function was potentially out of bounds. This is false b/c
it is forming an iterator range but it would be invalid if Init was a nullptr.

I add an assertion in the constructor for this and remove to FIXME checks that
are related to this. I checked the various usages and we always valid the
argument is not nullptr.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 7, 2025

@llvm/pr-subscribers-clang

Author: Shafik Yaghmour (shafik)

Changes

Static analysis complained that:

child_range(&Init, &Init+1);

in the children member function was potentially out of bounds. This is false b/c it is forming an iterator range but it would be invalid if Init was a nullptr.

I add an assertion in the constructor for this and remove to FIXME checks that are related to this. I checked the various usages and we always valid the argument is not nullptr.


Full diff: https://github.com/llvm/llvm-project/pull/152593.diff

1 Files Affected:

  • (modified) clang/include/clang/AST/Expr.h (+1-6)
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 237b3b2cf7444..a5116a72264b9 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -3548,6 +3548,7 @@ class CompoundLiteralExpr : public Expr {
                       QualType T, ExprValueKind VK, Expr *init, bool fileScope)
       : Expr(CompoundLiteralExprClass, T, VK, OK_Ordinary),
         LParenLoc(lparenloc), TInfoAndScope(tinfo, fileScope), Init(init) {
+    assert(Init && "Init is a nullptr");
     setDependence(computeDependence(this));
   }
 
@@ -3577,17 +3578,11 @@ class CompoundLiteralExpr : public Expr {
   APValue &getStaticValue() const;
 
   SourceLocation getBeginLoc() const LLVM_READONLY {
-    // FIXME: Init should never be null.
-    if (!Init)
-      return SourceLocation();
     if (LParenLoc.isInvalid())
       return Init->getBeginLoc();
     return LParenLoc;
   }
   SourceLocation getEndLoc() const LLVM_READONLY {
-    // FIXME: Init should never be null.
-    if (!Init)
-      return SourceLocation();
     return Init->getEndLoc();
   }
 

@github-actions
Copy link

github-actions bot commented Aug 7, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@shafik shafik merged commit 2936774 into llvm:main Aug 8, 2025
9 checks passed
@shafik shafik deleted the fix_compoundLiteralExpr_assert_init branch August 11, 2025 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants