]> granicus.if.org Git - clang/commitdiff
[Sema] Don't create an invalid source range for overlong initializer lists.
authorBenjamin Kramer <benny.kra@googlemail.com>
Wed, 23 Sep 2015 16:03:53 +0000 (16:03 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Wed, 23 Sep 2015 16:03:53 +0000 (16:03 +0000)
We took both source locations from the end of the initializer list what
the code below doesn't expect. This can lead to a crash when rendering
the diagnostic (PR24816). Assert that we have more than one element in
a scalar initializer with too many elements.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@248391 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaInit.cpp
test/SemaCXX/cxx0x-initializer-aggregates.cpp

index de11666747ab1744f6cfc4ede059e04ff761449a..74db24f28dcd01cbe4646d28ad6b0b7d532bc66e 100644 (file)
@@ -7047,10 +7047,12 @@ bool InitializationSequence::Diagnose(Sema &S,
     SourceRange R;
 
     auto *InitList = dyn_cast<InitListExpr>(Args[0]);
-    if (InitList && InitList->getNumInits() == 1)
+    if (InitList && InitList->getNumInits() >= 1) {
       R = SourceRange(InitList->getInit(0)->getLocEnd(), InitList->getLocEnd());
-    else
+    } else {
+      assert(Args.size() > 1 && "Expected multiple initializers!");
       R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd());
+    }
 
     R.setBegin(S.getLocForEndOfToken(R.getBegin()));
     if (Kind.isCStyleOrFunctionalCast())
index 0e9a97d5bb0721b469ac8fd6320bfdbf6fb11462..1b01a351f781749b3702560ec4ce9d5ec931684d 100644 (file)
@@ -129,3 +129,7 @@ namespace array_addressof {
   using T = int[5];
   T *p = &T{1,2,3,4,5}; // expected-error {{taking the address of a temporary object of type 'T' (aka 'int [5]')}}
 }
+
+namespace PR24816 {
+  struct { int i; } ne = {{0, 1}}; // expected-error{{excess elements in scalar initializer}}
+}