[PATCH] D124694: [randstruct] Move initializer check to be more effective

Bill Wendling via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 29 13:15:49 PDT 2022


void created this revision.
void added reviewers: stuij, MaskRay, aaron.ballman.
Herald added a subscriber: StephenFan.
Herald added a project: All.
void requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

If a randomized structure has an initializer with a dedicated
initializer in it, the field initialzed by that dedicated initializer
may end up at the end of the RecordDecl. This however may skip the
random layout initization check.

  struct t {
     int a, b, c, d, e;
  } x = { .a = 2, 4, 5, 6 };

Let's say that "a" is lands as the last field after randomization. The
call to CheckDesignatedInitializer sets the iterator to the end of the
initializer list. During the next iteration of the initializer list
check, it detects that and fails to issue the error about initializing
a randomized struct with non-designated initializer. Instead, it issues
an error about "excess elements in struct initializer", which is
confusing under these circumstances.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124694

Files:
  clang/lib/Sema/SemaInit.cpp
  clang/test/Sema/init-randomized-struct.c


Index: clang/test/Sema/init-randomized-struct.c
===================================================================
--- clang/test/Sema/init-randomized-struct.c
+++ clang/test/Sema/init-randomized-struct.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -triple=x86_64-unknown-linux -frandomize-layout-seed=1234567890abcdef \
+// RUN: %clang_cc1 -triple=x86_64-unknown-linux -frandomize-layout-seed=1234567890abcded \
 // RUN:  -verify -fsyntax-only -Werror %s
 
 // Initializing a randomized structure requires a designated initializer,
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -2170,11 +2170,6 @@
       continue;
     }
 
-    if (Field == FieldEnd) {
-      // We've run out of fields. We're done.
-      break;
-    }
-
     // Check if this is an initializer of forms:
     //
     //   struct foo f = {};
@@ -2204,6 +2199,11 @@
       break;
     }
 
+    if (Field == FieldEnd) {
+      // We've run out of fields. We're done.
+      break;
+    }
+
     // We've already initialized a member of a union. We're done.
     if (InitializedSomething && DeclType->isUnionType())
       break;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124694.426149.patch
Type: text/x-patch
Size: 1214 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220429/a4492d63/attachment-0001.bin>


More information about the cfe-commits mailing list