Skip to content

Commit 8936d30

Browse files
committed
[clang][analyzer] Fix a possible crash in CastSizeChecker
1 parent e696f4e commit 8936d30

File tree

2 files changed

+27
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)