Skip to content

Commit 026bcde

Browse files
committed
fix TryToFixInvalidVariablyModifiedType to reject negative array sizes
llvm-svn: 63557
1 parent a02e45c commit 026bcde

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,9 +3114,10 @@ static QualType TryToFixInvalidVariablyModifiedType(QualType T,
31143114

31153115
assert(EvalResult.Val.isInt() && "Size expressions must be integers!");
31163116
llvm::APSInt &Res = EvalResult.Val.getInt();
3117-
3118-
return Context.getConstantArrayType(VLATy->getElementType(),
3119-
Res, ArrayType::Normal, 0);
3117+
if (Res >= llvm::APSInt(Res.getBitWidth(), Res.isUnsigned()))
3118+
return Context.getConstantArrayType(VLATy->getElementType(),
3119+
Res, ArrayType::Normal, 0);
3120+
return QualType();
31203121
}
31213122

31223123
bool Sema::VerifyBitField(SourceLocation FieldLoc, IdentifierInfo *FieldName,

clang/test/Sema/struct-decl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ struct bar {
77

88
struct foo {
99
char name[(int)&((struct bar *)0)->n];
10+
char name2[(int)&((struct bar *)0)->n - 1]; //expected-error{{fields must have a constant size}}
1011
};

0 commit comments

Comments
 (0)