]> granicus.if.org Git - clang/commitdiff
Reorganize this loop a bit so it doesn't crash for empty unions. Fixes
authorEli Friedman <eli.friedman@gmail.com>
Fri, 6 Jun 2008 20:12:37 +0000 (20:12 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Fri, 6 Jun 2008 20:12:37 +0000 (20:12 +0000)
PR2419.

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

lib/CodeGen/CGExprConstant.cpp
test/CodeGen/empty-union-init.c [new file with mode: 0644]

index 58b18848171bf42cfbfe4f85036a5522be8565f7..f5977728af8dd697a28a0a12f88465c8eb75da9a 100644 (file)
@@ -213,13 +213,15 @@ public:
 
     // Find the field decl we're initializing, if any
     int FieldNo = 0; // Field no in RecordDecl
-    FieldDecl* curField;
-    do {
+    FieldDecl* curField = 0;
+    while (FieldNo < RD->getNumMembers()) {
       curField = RD->getMember(FieldNo);
       FieldNo++;
-    } while (!curField->getIdentifier() && FieldNo < RD->getNumMembers());
+      if (curField->getIdentifier())
+        break;
+    }
 
-    if (ILE->getNumInits() == 0 || !curField->getIdentifier())
+    if (!curField || !curField->getIdentifier() || ILE->getNumInits() == 0)
       return llvm::Constant::getNullValue(Ty);
 
     if (curField->isBitField()) {
diff --git a/test/CodeGen/empty-union-init.c b/test/CodeGen/empty-union-init.c
new file mode 100644 (file)
index 0000000..6b8def9
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: clang -emit-llvm < %s -o -
+
+struct Mem {
+        union {
+        } u;
+};
+
+struct Mem *columnMem(){
+        static const struct Mem nullMem = { {} };
+}
+
+