r181970 - Return QualType() when a too large array is attempting to be created. This

Richard Trieu rtrieu at google.com
Wed May 15 18:46:09 PDT 2013


Author: rtrieu
Date: Wed May 15 20:46:09 2013
New Revision: 181970

URL: http://llvm.org/viewvc/llvm-project?rev=181970&view=rev
Log:
Return QualType() when a too large array is attempting to be created.  This
prevents further errors and some overflows in size calculations.
One overflow was previously triggering an assert.

Modified:
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/Sema/offsetof-64.c

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=181970&r1=181969&r2=181970&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Wed May 15 20:46:09 2013
@@ -1549,10 +1549,12 @@ QualType Sema::BuildArrayType(QualType T
       // Is the array too large?
       unsigned ActiveSizeBits
         = ConstantArrayType::getNumAddressingBits(Context, T, ConstVal);
-      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context))
+      if (ActiveSizeBits > ConstantArrayType::getMaxSizeBits(Context)) {
         Diag(ArraySize->getLocStart(), diag::err_array_too_large)
           << ConstVal.toString(10)
           << ArraySize->getSourceRange();
+        return QualType();
+      }
     }
 
     T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);

Modified: cfe/trunk/test/Sema/offsetof-64.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/offsetof-64.c?rev=181970&r1=181969&r2=181970&view=diff
==============================================================================
--- cfe/trunk/test/Sema/offsetof-64.c (original)
+++ cfe/trunk/test/Sema/offsetof-64.c Wed May 15 20:46:09 2013
@@ -1,15 +1,22 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple x86_64-linux-gnu
-// expected-no-diagnostics
 
 // PR15216
 // Don't crash when taking computing the offset of structs with large arrays.
 const unsigned long Size = (1l << 62);
 
-struct Chunk {
+struct Chunk1 {
   char padding[Size];
   char more_padding[1][Size];
   char data;
 };
 
-int test1 = __builtin_offsetof(struct Chunk, data);
+int test1 = __builtin_offsetof(struct Chunk1, data);
 
+struct Chunk2 {
+  char padding[Size][Size][Size];  // expected-error 2{{array is too large}}
+  char data;
+};
+
+// FIXME: Remove this error when the constant evaluator learns to
+// ignore bad types.
+int test2 = __builtin_offsetof(struct Chunk2, data);  // expected-error{{initializer element is not a compile-time constant}} 





More information about the cfe-commits mailing list