Skip to content

Commit 4be37e6

Browse files
committed
[clang-tidy] Fix bugprone-sizeof-expression crash on arrays of dependent type
1 parent 82acc31 commit 4be37e6

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
378378
if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) {
379379
// check if the array element size is larger than one. If true,
380380
// the size of the array is higher than the number of elements
381-
CharUnits SSize = Ctx.getTypeSizeInChars(Type->getElementType());
382-
if (!SSize.isOne()) {
381+
if (!getSizeOfType(Ctx, Type).isOne()) {
383382
diag(SzOfExpr->getBeginLoc(),
384383
"suspicious usage of 'sizeof' in the loop")
385384
<< SzOfExpr->getSourceRange();

clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ void loop_access_elements(int num, struct B b) {
227227
for(int i = 0, j = 0; i < sizeof(arr) && j < sizeof(buf); i++, j++) {}
228228
}
229229

230+
template <typename T>
231+
void templated_array() {
232+
T arr[10];
233+
// CHECK-MESSAGES: :[[@LINE+1]]:23: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
234+
for (int i = 0; i < sizeof(arr); ++i) {}
235+
}
236+
230237
template <int T>
231238
int Foo() { int A[T]; return sizeof(T); }
232239
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: suspicious usage of 'sizeof(K)'

0 commit comments

Comments
 (0)