From 145032a57f7844c29f2c40415b46338ac3445207 Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Thu, 12 Aug 2010 21:35:19 +0300 Subject: [PATCH] Make object_key_t portable A flexible array member is unportable. Use a table of length 1 instead. This needs some adjustment to the memory allocatio, too. --- src/jansson_private.h | 2 +- src/value.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/jansson_private.h b/src/jansson_private.h index 6d7e46c..55532eb 100644 --- a/src/jansson_private.h +++ b/src/jansson_private.h @@ -53,7 +53,7 @@ typedef struct { typedef struct { unsigned long serial; - char key[]; + char key[1]; } object_key_t; const object_key_t *jsonp_object_iter_fullkey(void *iter); diff --git a/src/value.c b/src/value.c index e024fdb..8e0cfa2 100644 --- a/src/value.c +++ b/src/value.c @@ -9,6 +9,7 @@ #include +#include #include #include @@ -124,9 +125,11 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) } object = json_to_object(json); - k = malloc(sizeof(object_key_t) + strlen(key) + 1); - if(!k) - return -1; + /* offsetof(...) returns the size of object_key_t without the + last, flexible member. This way, the correct amount is + allocated. */ + k = malloc(offsetof(object_key_t, key) + + strlen(key) + 1); if(!k) return -1; k->serial = object->serial++; strcpy(k->key, key); -- 2.7.4