From: Aaron Ballman Date: Thu, 9 Feb 2012 03:29:06 +0000 (+0000) Subject: Attempting to initialize a union member that does not exist no longer crashes. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0ca7e8bf904d1c2cf70d271f3a06c1d71ff7e4fb;p=clang Attempting to initialize a union member that does not exist no longer crashes. Patch by Remi Gacogne git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150144 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaInit.cpp b/lib/Sema/SemaInit.cpp index ece019c3a7..759fb16d1a 100644 --- a/lib/Sema/SemaInit.cpp +++ b/lib/Sema/SemaInit.cpp @@ -1511,7 +1511,8 @@ static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, IdentifierInfo *FieldName) { assert(AnonField->isAnonymousStructOrUnion()); Decl *NextDecl = AnonField->getNextDeclInContext(); - while (IndirectFieldDecl *IF = dyn_cast(NextDecl)) { + IndirectFieldDecl *IF = NULL; + while (NextDecl && (IF = dyn_cast(NextDecl))) { if (FieldName && FieldName == IF->getAnonField()->getIdentifier()) return IF; NextDecl = NextDecl->getNextDeclInContext(); diff --git a/test/Sema/init.c b/test/Sema/init.c index 2527e14fcb..4dcfafa479 100644 --- a/test/Sema/init.c +++ b/test/Sema/init.c @@ -18,10 +18,19 @@ extern int x; void *g = &x; int *h = &x; +struct union_crash +{ + union + { + }; +}; + int test() { -int a[10]; -int b[10] = a; // expected-error {{array initializer must be an initializer list}} -int +; // expected-error {{expected identifier or '('}} + int a[10]; + int b[10] = a; // expected-error {{array initializer must be an initializer list}} + int +; // expected-error {{expected identifier or '('}} + + struct union_crash u = { .d = 1 }; // expected-error {{field designator 'd' does not refer to any field in type 'struct union_crash'}} }