[clang] 36a4a55 - [Clang] Fix crash for _Countof(void) by correcting extension handling (#181169)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 13 05:38:41 PST 2026


Author: Aadarsh Keshri
Date: 2026-02-13T13:38:35Z
New Revision: 36a4a55f580bc6fe7a86bf4b5ea447925c100197

URL: https://github.com/llvm/llvm-project/commit/36a4a55f580bc6fe7a86bf4b5ea447925c100197
DIFF: https://github.com/llvm/llvm-project/commit/36a4a55f580bc6fe7a86bf4b5ea447925c100197.diff

LOG: [Clang] Fix crash for _Countof(void) by correcting extension handling (#181169)

Fix a crash for _Countof(void) caused by incorrect extension handling in
CheckExtensionTraitOperandType.

Fixes #180893

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaExpr.cpp
    clang/test/C/C2y/n3369.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index dcb27c25d7110..110761081e796 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -261,6 +261,7 @@ Bug Fixes in This Version
 - Fixes an assertion failure when evaluating ``__underlying_type`` on enum redeclarations. (#GH177943)
 
 - Clang now outputs relative paths of embeds for dependency output. (#GH161950)
+- Fixed an assertion failure when evaluating ``_Countof`` on invalid ``void``-typed operands. (#GH180893)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index fce803fcdd395..6f4a2219c666a 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -4225,24 +4225,25 @@ static bool CheckExtensionTraitOperandType(Sema &S, QualType T,
     return true;
 
   // C99 6.5.3.4p1:
-  if (T->isFunctionType() &&
-      (TraitKind == UETT_SizeOf || TraitKind == UETT_AlignOf ||
-       TraitKind == UETT_PreferredAlignOf)) {
+  if (TraitKind == UETT_SizeOf || TraitKind == UETT_AlignOf ||
+      TraitKind == UETT_PreferredAlignOf) {
+
     // sizeof(function)/alignof(function) is allowed as an extension.
-    S.Diag(Loc, diag::ext_sizeof_alignof_function_type)
-        << getTraitSpelling(TraitKind) << ArgRange;
-    return false;
-  }
+    if (T->isFunctionType()) {
+      S.Diag(Loc, diag::ext_sizeof_alignof_function_type)
+          << getTraitSpelling(TraitKind) << ArgRange;
+      return false;
+    }
 
-  // Allow sizeof(void)/alignof(void) as an extension, unless in OpenCL where
-  // this is an error (OpenCL v1.1 s6.3.k)
-  if (T->isVoidType()) {
-    unsigned DiagID = S.LangOpts.OpenCL ? diag::err_opencl_sizeof_alignof_type
-                                        : diag::ext_sizeof_alignof_void_type;
-    S.Diag(Loc, DiagID) << getTraitSpelling(TraitKind) << ArgRange;
-    return false;
+    // Allow sizeof(void)/alignof(void) as an extension, unless in OpenCL where
+    // this is an error (OpenCL v1.1 s6.3.k)
+    if (T->isVoidType()) {
+      unsigned DiagID = S.LangOpts.OpenCL ? diag::err_opencl_sizeof_alignof_type
+                                          : diag::ext_sizeof_alignof_void_type;
+      S.Diag(Loc, DiagID) << getTraitSpelling(TraitKind) << ArgRange;
+      return false;
+    }
   }
-
   return true;
 }
 

diff  --git a/clang/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index dd3125709279d..64c55e3a9adce 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -35,6 +35,10 @@ void test_semantic_failures() {
   int non_array;
   (void)_Countof non_array;  // expected-error {{'_Countof' requires an argument of array type; 'int' invalid}}  
   (void)_Countof(int);       // expected-error {{'_Countof' requires an argument of array type; 'int' invalid}}  
+  (void)_Countof(void);      // expected-error {{invalid application of '_Countof' to an incomplete type 'void'}}
+  int arr2[_Countof(void)];  // expected-error {{invalid application of '_Countof' to an incomplete type 'void'}}
+  void *vp = &non_array;
+  (void)_Countof(*vp);       // expected-error {{invalid application of '_Countof' to an incomplete type 'void'}}
   (void)_Countof(test_semantic_failures); // expected-error {{invalid application of '_Countof' to a function type}}
   (void)_Countof(struct S);  // expected-error {{invalid application of '_Countof' to an incomplete type 'struct S'}} \
                                 expected-note {{forward declaration of 'struct S'}}


        


More information about the cfe-commits mailing list