]> granicus.if.org Git - clang/commitdiff
fix TryToFixInvalidVariablyModifiedType to reject negative array sizes
authorNuno Lopes <nunoplopes@sapo.pt>
Mon, 2 Feb 2009 22:32:08 +0000 (22:32 +0000)
committerNuno Lopes <nunoplopes@sapo.pt>
Mon, 2 Feb 2009 22:32:08 +0000 (22:32 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63557 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/Sema/struct-decl.c

index d03879e907e5bbe3c091e26087c4e5d61f0d71a9..21fe6e1ce4924e0538c6388041980d0cbfe72d47 100644 (file)
@@ -3114,9 +3114,10 @@ static QualType TryToFixInvalidVariablyModifiedType(QualType T,
     
   assert(EvalResult.Val.isInt() && "Size expressions must be integers!");
   llvm::APSInt &Res = EvalResult.Val.getInt();
-
-  return Context.getConstantArrayType(VLATy->getElementType(),
-                                      Res, ArrayType::Normal, 0);
+  if (Res >= llvm::APSInt(Res.getBitWidth(), Res.isUnsigned()))
+    return Context.getConstantArrayType(VLATy->getElementType(),
+                                        Res, ArrayType::Normal, 0);
+  return QualType();
 }
 
 bool Sema::VerifyBitField(SourceLocation FieldLoc, IdentifierInfo *FieldName, 
index cacd847658261d478331441875edb6e3c765e601..7d7961b16c834ebee5f0f4e5729b3957697e2d83 100644 (file)
@@ -7,4 +7,5 @@ struct bar {
 
 struct foo {
        char name[(int)&((struct bar *)0)->n];
+       char name2[(int)&((struct bar *)0)->n - 1]; //expected-error{{fields must have a constant size}}
 };