Skip to content

Conversation

AaronBallman
Copy link
Collaborator

sizeof was handled correctly, but __datasizeof and _Countof were not.

Fixes #151711

sizeof was handled correctly, but __datasizeof and _Countof were
not.

Fixes llvm#151711
@AaronBallman AaronBallman added clang Clang issues not falling into any other category c++ clang:static analyzer crash-on-valid c2y labels Aug 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 1, 2025

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Aaron Ballman (AaronBallman)

Changes

sizeof was handled correctly, but __datasizeof and _Countof were not.

Fixes #151711


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp (+2-1)
  • (added) clang/test/Analysis/engine/gh151711.cpp (+18)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4a2edae7509de..69b5605df52d6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -241,6 +241,8 @@ Static Analyzer
 ---------------
 - The Clang Static Analyzer now handles parenthesized initialization.
   (#GH148875)
+- ``__datasizeof`` (C++) and ``_Countof`` (C) no longer cause a failed assertion
+  when given an operand of VLA type. (#GH151711)
 
 New features
 ^^^^^^^^^^^^
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
index f1a25a750dd0d..4ddf8fd5b4b0f 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
@@ -868,7 +868,8 @@ VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
   QualType T = Ex->getTypeOfArgument();
 
   for (ExplodedNode *N : CheckedSet) {
-    if (Ex->getKind() == UETT_SizeOf) {
+    if (Ex->getKind() == UETT_SizeOf || Ex->getKind() == UETT_DataSizeOf ||
+        Ex->getKind() == UETT_CountOf) {
       if (!T->isIncompleteType() && !T->isConstantSizeType()) {
         assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
 
diff --git a/clang/test/Analysis/engine/gh151711.cpp b/clang/test/Analysis/engine/gh151711.cpp
new file mode 100644
index 0000000000000..8d8488e3bc1f8
--- /dev/null
+++ b/clang/test/Analysis/engine/gh151711.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify -x c -std=c2y %s
+// expected-no-diagnostics
+
+// Ensure that VLA types are correctly handled by unary type traits in the
+// expression engine. Previously, __datasizeof and _Countof both caused failed
+// assertions.
+void gh151711(int i) {
+  (void)sizeof(int[i++]);
+
+#ifdef __cplusplus
+  // __datasizeof is only available in C++.
+  (void)__datasizeof(int[i++]);
+#else
+  // _Countof is only available in C.
+  (void)_Countof(int[i++]);
+#endif
+}

@llvmbot
Copy link
Member

llvmbot commented Aug 1, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

sizeof was handled correctly, but __datasizeof and _Countof were not.

Fixes #151711


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp (+2-1)
  • (added) clang/test/Analysis/engine/gh151711.cpp (+18)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4a2edae7509de..69b5605df52d6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -241,6 +241,8 @@ Static Analyzer
 ---------------
 - The Clang Static Analyzer now handles parenthesized initialization.
   (#GH148875)
+- ``__datasizeof`` (C++) and ``_Countof`` (C) no longer cause a failed assertion
+  when given an operand of VLA type. (#GH151711)
 
 New features
 ^^^^^^^^^^^^
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
index f1a25a750dd0d..4ddf8fd5b4b0f 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp
@@ -868,7 +868,8 @@ VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
   QualType T = Ex->getTypeOfArgument();
 
   for (ExplodedNode *N : CheckedSet) {
-    if (Ex->getKind() == UETT_SizeOf) {
+    if (Ex->getKind() == UETT_SizeOf || Ex->getKind() == UETT_DataSizeOf ||
+        Ex->getKind() == UETT_CountOf) {
       if (!T->isIncompleteType() && !T->isConstantSizeType()) {
         assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
 
diff --git a/clang/test/Analysis/engine/gh151711.cpp b/clang/test/Analysis/engine/gh151711.cpp
new file mode 100644
index 0000000000000..8d8488e3bc1f8
--- /dev/null
+++ b/clang/test/Analysis/engine/gh151711.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
+// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify -x c -std=c2y %s
+// expected-no-diagnostics
+
+// Ensure that VLA types are correctly handled by unary type traits in the
+// expression engine. Previously, __datasizeof and _Countof both caused failed
+// assertions.
+void gh151711(int i) {
+  (void)sizeof(int[i++]);
+
+#ifdef __cplusplus
+  // __datasizeof is only available in C++.
+  (void)__datasizeof(int[i++]);
+#else
+  // _Countof is only available in C.
+  (void)_Countof(int[i++]);
+#endif
+}

Copy link
Contributor

@steakhal steakhal left a comment

Choose a reason for hiding this comment

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

Hey, thanks for the fix.

@steakhal
Copy link
Contributor

steakhal commented Aug 1, 2025

Hold on, the issue you fixed here was only opened like 30 minutes ago, how did you patch this in just 10 minutes?

@AaronBallman
Copy link
Collaborator Author

Hold on, the issue you fixed here was only opened like 30 minutes ago, how did you patch this in just 10 minutes?

I happened to triage the issue and went "I wonder what's going on?" then saw what was happening and realized "I just had to do this dance for _Countof and I think I missed this spot". :-D

Copy link
Contributor

@steakhal steakhal left a comment

Choose a reason for hiding this comment

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

Please merge this once the CI is green. Thank you!

@AaronBallman AaronBallman merged commit 1732748 into llvm:main Aug 1, 2025
10 checks passed
@AaronBallman AaronBallman deleted the aballman-gh151711 branch August 1, 2025 16:32
krishna2803 pushed a commit to krishna2803/llvm-project that referenced this pull request Aug 12, 2025
…vm#151719)

sizeof was handled correctly, but __datasizeof and _Countof were not.

Fixes llvm#151711
@steakhal
Copy link
Contributor

/cherry-pick 1732748

@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

/cherry-pick 1732748

Error: Command failed due to missing milestone.

@steakhal
Copy link
Contributor

/cherry-pick 1732748

@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

Failed to cherry-pick: 1732748

https://github.com/llvm/llvm-project/actions/runs/17107761517

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

@tru tru moved this from Needs Triage to Needs Backport PR in LLVM Release Status Aug 21, 2025
steakhal pushed a commit to steakhal/llvm-project that referenced this pull request Aug 21, 2025
…vm#151719)

sizeof was handled correctly, but __datasizeof and _Countof were not.

Fixes llvm#151711

(cherry picked from commit 1732748 with adjustments)
Dropping the ReleaseNotes part of the original patch.
@steakhal
Copy link
Contributor

The clang-21 backport PR is #154738.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs Backport PR
Development

Successfully merging this pull request may close these issues.

[analyzer] crashes on __datasizeof
4 participants