Author: Tadeusz
Date: 2026-08-27T14:59:04+03:00
New Revision: 5c52676a51a88b3621a74651435f5650679b344c

URL: 
https://github.com/llvm/llvm-project/commit/5c52676a51a88b3621a74651435f5650679b344c
DIFF: 
https://github.com/llvm/llvm-project/commit/5c52676a51a88b3621a74651435f5650679b344c.diff

LOG: [Clang][C] Fix init statement nullptr dereference in condition of while 
statement (#219056)

In conditions with an empty init statement with attributes such as
'([[]];)', the InitStmt pointer argument in the ParseCondition method
will be dereferenced. However, InitStmt is set to nullptr when parsing a
'while' statement condition, causing a crash.

This change fixes the issue by added a check that InitStmt is not
nullptr before entering the associated logic, which is consistent with
how other parts of this function handle a nullptr InitStmt. This makes
sense because a nullptr InitStmt is associated with conditions that
cannot have an init statement (the only case being 'while') and this
block operates exclusively on init statements.

Fix #218879

Added: 
    

Modified: 
    clang/lib/Parse/ParseExprCXX.cpp
    clang/test/Parser/c2x-attributes.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/ParseExprCXX.cpp 
b/clang/lib/Parse/ParseExprCXX.cpp
index f9a0dcc7d53af..4564e4dc364f0 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -1936,7 +1936,7 @@ Sema::ConditionResult Parser::ParseCondition(StmtResult 
*InitStmt,
 
     // Handle '(; expr)', '([[...]]; expr)' and '(__attribute__((...)); expr)'
     // when GNU-style attributes are finalized.
-    if (Tok.is(tok::semi)) {
+    if (InitStmt && Tok.is(tok::semi)) {
       StmtResult Null = Actions.ActOnNullStmt(ConsumeToken());
       if (ParsedAttrs) {
         WarnOnInit();

diff  --git a/clang/test/Parser/c2x-attributes.c 
b/clang/test/Parser/c2x-attributes.c
index b291cb75818c0..5ecb2ec0dab74 100644
--- a/clang/test/Parser/c2x-attributes.c
+++ b/clang/test/Parser/c2x-attributes.c
@@ -120,6 +120,12 @@ void f11(void) {
 
   [[]] for (;;);
   [[]] while (1);
+
+  while ([[]];) {} // expected-error {{an attribute list cannot appear here}} \
+                   // expected-error {{expected expression}}
+  while (;[[]];;) {} // expected-error {{expected expression}} \
+                     // expected-error {{expected expression}}
+
   [[]] do [[]] { } while(1);
 
   [[]] (void)1;


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to