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

Aadarsh Keshri via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 12 12:49:32 PST 2026


https://github.com/Aadarsh-Keshri updated https://github.com/llvm/llvm-project/pull/181169

>From 3eab75b64942ef3814312aae8ec67f141544a825 Mon Sep 17 00:00:00 2001
From: Aadarsh-Keshri <keshri.aadarsh123 at gmail.com>
Date: Thu, 12 Feb 2026 20:48:12 +0530
Subject: [PATCH 1/2] [Clang] Fix crash for _Countof(void) by correcting
 extension handling

---
 clang/lib/Sema/SemaExpr.cpp | 31 ++++++++++++++++---------------
 clang/test/C/C2y/n3369.c    |  2 ++
 2 files changed, 18 insertions(+), 15 deletions(-)

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..eee723bbb33b7 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -35,6 +35,8 @@ 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)_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'}}

>From e4229110f74b50aa6bd2f1bd7a670925ff6d6431 Mon Sep 17 00:00:00 2001
From: Aadarsh-Keshri <keshri.aadarsh123 at gmail.com>
Date: Fri, 13 Feb 2026 01:08:33 +0530
Subject: [PATCH 2/2] Updated clang/docs/ReleaseNotes.rst and added a test for
 expression operands

---
 clang/docs/ReleaseNotes.rst | 1 +
 clang/test/C/C2y/n3369.c    | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 83cd562c6f49b..08cfda6a1ff15 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/test/C/C2y/n3369.c b/clang/test/C/C2y/n3369.c
index eee723bbb33b7..64c55e3a9adce 100644
--- a/clang/test/C/C2y/n3369.c
+++ b/clang/test/C/C2y/n3369.c
@@ -37,6 +37,8 @@ void test_semantic_failures() {
   (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