]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: avoid use of fake flex arrays
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 5 Feb 2023 20:15:52 +0000 (21:15 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 6 Feb 2023 11:04:16 +0000 (12:04 +0100)
I tried to use DECLARE_FLEX_ARRAY like the kernel does, but it does not work
for anonymous structs (they cannot be declared inline), so an open-coded
version is used.

src/fundamental/macro-fundamental.h
src/libsystemd/sd-journal/journal-def.h

index b939ee1a7e97d61c814c287bcec42945c67f3c22..c4ad957588881489e88a72bf11309e37731371db 100644 (file)
@@ -376,3 +376,15 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
         (v) = UPDATE_FLAG(v, flag, b)
 #define FLAGS_SET(v, flags) \
         ((~(v) & (flags)) == 0)
+
+/* Declare a flexible array usable in a union.
+ * This is essentially a work-around for a pointless constraint in C99
+ * and might go away in some future version of the standard.
+ *
+ * See https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=3080ea5553cc909b000d1f1d964a9041962f2c5b
+ */
+#define DECLARE_FLEX_ARRAY(type, name)                 \
+        struct {                                       \
+                dummy_t __empty__ ## name;             \
+                type name[];                           \
+        }
index ab4880761b3b6a71aa7c3354d94eef26c1725d2e..d35290d3c70ba30f507885a6343a742d96e6dc73 100644 (file)
@@ -93,22 +93,28 @@ struct FieldObject FieldObject__contents;
 struct FieldObject__packed FieldObject__contents _packed_;
 assert_cc(sizeof(struct FieldObject) == sizeof(struct FieldObject__packed));
 
-#define EntryObject__contents {                 \
-        ObjectHeader object;                    \
-        le64_t seqnum;                          \
-        le64_t realtime;                        \
-        le64_t monotonic;                       \
-        sd_id128_t boot_id;                     \
-        le64_t xor_hash;                        \
-        union {                                 \
-                struct {                        \
-                        le64_t object_offset;   \
-                        le64_t hash;            \
-                } regular[0];                   \
-                struct {                        \
-                        le32_t object_offset;   \
-                } compact[0];                   \
-        } items;                                \
+#define EntryObject__contents {                        \
+        ObjectHeader object;                           \
+        le64_t seqnum;                                 \
+        le64_t realtime;                               \
+        le64_t monotonic;                              \
+        sd_id128_t boot_id;                            \
+        le64_t xor_hash;                               \
+        union {                                        \
+                struct {                               \
+                        dummy_t __empty__regular;      \
+                        struct {                       \
+                                le64_t object_offset;  \
+                                le64_t hash;           \
+                        } regular[];                   \
+                };                                     \
+                struct {                               \
+                        dummy_t __empty_compact;       \
+                        struct {                       \
+                                le32_t object_offset;  \
+                        } compact[];                   \
+                };                                     \
+        } items;                                       \
 }
 
 struct EntryObject EntryObject__contents;
@@ -129,8 +135,8 @@ struct EntryArrayObject {
         ObjectHeader object;
         le64_t next_entry_array_offset;
         union {
-                le64_t regular[0];
-                le32_t compact[0];
+                DECLARE_FLEX_ARRAY(le64_t, regular);
+                DECLARE_FLEX_ARRAY(le32_t, compact);
         } items;
 } _packed_;