]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
marshal: use zero-sized array instead of flexible arrays
authorVincent Bernat <bernat@luffy.cx>
Fri, 4 May 2012 09:48:40 +0000 (11:48 +0200)
committerVincent Bernat <bernat@luffy.cx>
Fri, 4 May 2012 09:48:40 +0000 (11:48 +0200)
With gcc 2.96, flexible arrays are not supported, even with
-std=gnu99. And with gcc 3.x, zero-sized arrays cannot be statically
initialized (with more than one element).

src/display.c
src/marshal.c
src/marshal.h

index b2cbd8adb351aa81c3c572bd91b909d1d5d719ac..892ebedb833e6736092750803fb6651054ec3fc4 100644 (file)
@@ -901,13 +901,14 @@ display_pids(struct writer *w, struct lldpd_port *port)
 {
        struct lldpd_pi *pi;
        char *hex;
+       int i;
        TAILQ_FOREACH(pi, &port->p_pids, p_entries) {
                if (!pi->p_pi_len) continue;
                tag_start(w, "pi", "PI");
                /* Convert to hex for display */
                if ((hex = malloc(pi->p_pi_len * 2 + 1)) == NULL)
                        fatal(NULL);
-               for (int i = 0; i < pi->p_pi_len; i++)
+               for (i = 0; i < pi->p_pi_len; i++)
                        snprintf(hex + 2*i, 3, "%02X", (unsigned char)pi->p_pi[i]);
                tag_data(w, hex);
                tag_end(w);
index ba03950dcab79b2313734ae11cea7db203f903b9..09ffa490bf309a1081fa29aac4fe8bb3dbac73dc 100644 (file)
@@ -27,17 +27,17 @@ struct marshal_serialized {
 struct marshal_info marshal_info_string = {
        .name = "null string",
        .size = 0,
-       .pointers = {{ .mi = NULL }},
+       .pointers = {MARSHAL_SUBINFO_NULL},
 };
 struct marshal_info marshal_info_fstring = {
        .name = "fixed string",
        .size = 0,
-       .pointers = {{ .mi = NULL }},
+       .pointers = {MARSHAL_SUBINFO_NULL},
 };
 struct marshal_info marshal_info_ignore = {
        .name = "ignored",
        .size = 0,
-       .pointers = {{ .mi = NULL }},
+       .pointers = {MARSHAL_SUBINFO_NULL},
 };
 
 /* List of already seen pointers */
index 91b99d5d5d094d29e24c531f3c380237bc17555a..a72272f85e2de9ade60edc1a107dec2c0c1d8a31 100644 (file)
@@ -31,10 +31,18 @@ struct marshal_subinfo {
        enum marshal_subinfo_kind kind; /* Kind of substructure */
        struct  marshal_info *mi;
 };
+#define MARSHAL_SUBINFO_NULL { .offset = 0, .offset2 = 0, .kind = ignore, .mi = NULL }
 struct marshal_info {
        char   *name;           /* Name of structure */
        size_t  size;           /* Size of the structure */
+#if defined __GNUC__ && __GNUC__ < 3
+       /* With gcc 2.96, flexible arrays are not supported, even with
+        * -std=gnu99. And with gcc 3.x, zero-sized arrays cannot be statically
+        * initialized (with more than one element). */
+       struct marshal_subinfo pointers[0]; /* Pointer to other structures */
+#else
        struct marshal_subinfo pointers[]; /* Pointer to other structures */
+#endif
 };
 /* Special case for strings */
 extern struct marshal_info marshal_info_string;
@@ -52,6 +60,7 @@ extern struct marshal_info marshal_info_ignore;
                .pointers = {
 #define MARSHAL_ADD(_kind, type, subtype, member)              \
        { .offset = offsetof(struct type, member),              \
+         .offset2 = 0,                                         \
          .kind = _kind,                                        \
          .mi = &MARSHAL_INFO(subtype) },
 #define MARSHAL_FSTR(type, member, len)                                \
@@ -59,7 +68,7 @@ extern struct marshal_info marshal_info_ignore;
          .offset2 = offsetof(struct type, len),                \
          .kind = pointer,                                      \
          .mi = &marshal_info_fstring },
-#define MARSHAL_END { .mi = NULL } } }
+#define MARSHAL_END MARSHAL_SUBINFO_NULL }}
 #else
 #define MARSHAL_BEGIN(type) extern struct marshal_info MARSHAL_INFO(type)
 #define MARSHAL_ADD(...)