[clang-tools-extra] [clang-tidy] Fix `bugprone-sizeof-expression` crash on arrays of dependent type (PR #159701)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 18 21:58:57 PDT 2025
https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/159701
>From 4be37e6a2d9964238ff6d67f5ea2883c79123c84 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 19 Sep 2025 04:26:30 +0000
Subject: [PATCH 1/3] [clang-tidy] Fix `bugprone-sizeof-expression` crash on
arrays of dependent type
---
.../clang-tidy/bugprone/SizeofExpressionCheck.cpp | 3 +--
.../clang-tidy/checkers/bugprone/sizeof-expression.cpp | 7 +++++++
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 139213ed359ba..2455b513f570f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -378,8 +378,7 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) {
// check if the array element size is larger than one. If true,
// the size of the array is higher than the number of elements
- CharUnits SSize = Ctx.getTypeSizeInChars(Type->getElementType());
- if (!SSize.isOne()) {
+ if (!getSizeOfType(Ctx, Type).isOne()) {
diag(SzOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof' in the loop")
<< SzOfExpr->getSourceRange();
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
index 33cf1cbea8377..f83d5ed052b77 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
@@ -227,6 +227,13 @@ void loop_access_elements(int num, struct B b) {
for(int i = 0, j = 0; i < sizeof(arr) && j < sizeof(buf); i++, j++) {}
}
+template <typename T>
+void templated_array() {
+ T arr[10];
+ // CHECK-MESSAGES: :[[@LINE+1]]:23: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
+ for (int i = 0; i < sizeof(arr); ++i) {}
+}
+
template <int T>
int Foo() { int A[T]; return sizeof(T); }
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: suspicious usage of 'sizeof(K)'
>From 736347b61704580725c76293cc25a40227b41008 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 19 Sep 2025 04:47:32 +0000
Subject: [PATCH 2/3] Do it properly, also add release notes
---
.../clang-tidy/bugprone/SizeofExpressionCheck.cpp | 2 +-
clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 2455b513f570f..cdb6a088b9d0a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -378,7 +378,7 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) {
// check if the array element size is larger than one. If true,
// the size of the array is higher than the number of elements
- if (!getSizeOfType(Ctx, Type).isOne()) {
+ if (!getSizeOfType(Ctx, Type->getElementType().getTypePtr()).isOne()) {
diag(SzOfExpr->getBeginLoc(),
"suspicious usage of 'sizeof' in the loop")
<< SzOfExpr->getSourceRange();
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3f403c42a168a..6b3e61e51c868 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -231,6 +231,10 @@ Changes in existing checks
<clang-tidy/checks/bugprone/signed-char-misuse>` check by fixing
false positives on C23 enums with the fixed underlying type of signed char.
+- Improved :doc:`bugprone-sizeof-expression
+ <clang-tidy/checks/bugprone/sizeof-expression>` check by fixing
+ a crash on ``sizeof`` of an array of template type.
+
- Improved :doc:`bugprone-tagged-union-member-count
<clang-tidy/checks/bugprone/tagged-union-member-count>` by fixing a false
positive when enums or unions from system header files or the ``std``
>From 1a2e3fd6993e84d89d95a2fe5a9a9f1fac2ec9f2 Mon Sep 17 00:00:00 2001
From: Victor Chernyakin <chernyakin.victor.j at outlook.com>
Date: Fri, 19 Sep 2025 04:58:45 +0000
Subject: [PATCH 3/3] Windows why
---
.../test/clang-tidy/checkers/bugprone/sizeof-expression.cpp | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
index f83d5ed052b77..e47f8b06f83c9 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
@@ -1,4 +1,6 @@
-// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- -config="{CheckOptions: {bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression: true}}" --
+// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t \
+// RUN: -- -config="{CheckOptions: {bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression: true}}" \
+// RUN: -- -fno-delayed-template-parsing
class C {
int size() { return sizeof(this); }
More information about the cfe-commits
mailing list