From 01d467defd2ff68a270ed23d9a619aa22d6a02f6 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Fri, 4 May 2012 11:48:40 +0200 Subject: [PATCH] marshal: use zero-sized array instead of flexible arrays 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 | 3 ++- src/marshal.c | 6 +++--- src/marshal.h | 11 ++++++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/display.c b/src/display.c index b2cbd8ad..892ebedb 100644 --- a/src/display.c +++ b/src/display.c @@ -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); diff --git a/src/marshal.c b/src/marshal.c index ba03950d..09ffa490 100644 --- a/src/marshal.c +++ b/src/marshal.c @@ -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 */ diff --git a/src/marshal.h b/src/marshal.h index 91b99d5d..a72272f8 100644 --- a/src/marshal.h +++ b/src/marshal.h @@ -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(...) -- 2.39.5