Skip to content

Commit 31ef7ac

Browse files
authored
[clang][analyzer] Fix a possible crash in CastSizeChecker (#134387)
1 parent 87a4215 commit 31ef7ac

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

clang/lib/StaticAnalyzer/Checkers/CastSizeChecker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ static bool evenFlexibleArraySize(ASTContext &Ctx, CharUnits RegionSize,
6262
assert(Last && "empty structs should already be handled");
6363

6464
const Type *ElemType = Last->getType()->getArrayElementTypeNoTypeQual();
65+
if (!ElemType)
66+
return false;
6567
CharUnits FlexSize;
6668
if (const ConstantArrayType *ArrayTy =
6769
Ctx.getAsConstantArrayType(Last->getType())) {

clang/test/Analysis/castsize.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_analyze_cc1 -verify %s \
2+
// RUN: -analyzer-checker=core,unix.Malloc,alpha.core.CastSize
3+
4+
typedef typeof(sizeof(int)) size_t;
5+
void *malloc(size_t);
6+
7+
struct s1 {
8+
int a;
9+
char x[];
10+
};
11+
12+
struct s2 {
13+
int a[100];
14+
char x[];
15+
};
16+
17+
union u {
18+
struct s1 a;
19+
struct s2 b;
20+
};
21+
22+
static union u *test() {
23+
union u *req;
24+
req = malloc(5); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
25+
return req;
26+
}

0 commit comments

Comments
 (0)