X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=src%2Fvalue.c;h=3895882c7bf77ac21d277bc5a6c8455f257e0e4b;hb=56643d4311ecb26001527520da6264d92eab3d76;hp=0f2ae65540e6464b0dd2d7461860ab554ff07d17;hpb=ffbab6fedd86ee1a0c7884d62fa7233ce9d745d2;p=jansson.git diff --git a/src/value.c b/src/value.c index 0f2ae65..3895882 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);