[clang][analyzer] Fix a possible crash in CastSizeChecker (#134387)

This commit is contained in:
Balázs Kéri
2025-04-07 09:46:03 +02:00
committed by GitHub
parent 87a4215ed1
commit 31ef7acf12
2 changed files with 28 additions and 0 deletions

View File

@@ -62,6 +62,8 @@ static bool evenFlexibleArraySize(ASTContext &Ctx, CharUnits RegionSize,
assert(Last && "empty structs should already be handled");
const Type *ElemType = Last->getType()->getArrayElementTypeNoTypeQual();
if (!ElemType)
return false;
CharUnits FlexSize;
if (const ConstantArrayType *ArrayTy =
Ctx.getAsConstantArrayType(Last->getType())) {

View File

@@ -0,0 +1,26 @@
// RUN: %clang_analyze_cc1 -verify %s \
// RUN: -analyzer-checker=core,unix.Malloc,alpha.core.CastSize
typedef typeof(sizeof(int)) size_t;
void *malloc(size_t);
struct s1 {
int a;
char x[];
};
struct s2 {
int a[100];
char x[];
};
union u {
struct s1 a;
struct s2 b;
};
static union u *test() {
union u *req;
req = malloc(5); // expected-warning{{Cast a region whose size is not a multiple of the destination type size}}
return req;
}