diff options
author | Petri Lehtinen <petri@digip.org> | 2009-09-06 22:24:55 +0300 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2009-09-06 22:24:55 +0300 |
commit | 234ee4728124d8d07412d159b45dd10854b6d6d8 (patch) | |
tree | 01c5714c029aed649720a09f5d1d85d28ebcbf9d /src | |
parent | 98a8c1aebff7a414f0830bc4f6b7bee19405f865 (diff) | |
download | jansson-234ee4728124d8d07412d159b45dd10854b6d6d8.zip jansson-234ee4728124d8d07412d159b45dd10854b6d6d8.tar.gz jansson-234ee4728124d8d07412d159b45dd10854b6d6d8.tar.bz2 |
Better argument validation
All pointer arguments are now tested for NULL. json_string() now also
tests that strdup() succeeds. This is to ensure that no NULL values
end up in data structures.
Also desribe the different sources of errors in documentation.
Diffstat (limited to 'src')
-rw-r--r-- | src/value.c | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/value.c b/src/value.c index d83d575..c84bfd3 100644 --- a/src/value.c +++ b/src/value.c @@ -122,6 +122,9 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { json_object_t *object; + if(!key || !value) + return -1; + if(!json_is_object(json)) { json_decref(value); @@ -255,6 +258,10 @@ json_t *json_array_get(const json_t *json, unsigned int index) int json_array_set_new(json_t *json, unsigned int index, json_t *value) { json_array_t *array; + + if(!value) + return -1; + if(!json_is_array(json)) { json_decref(value); @@ -277,6 +284,10 @@ int json_array_set_new(json_t *json, unsigned int index, json_t *value) int json_array_append_new(json_t *json, json_t *value) { json_array_t *array; + + if(!value) + return -1; + if(!json_is_array(json)) { json_decref(value); @@ -305,18 +316,28 @@ int json_array_append_new(json_t *json, json_t *value) json_t *json_string_nocheck(const char *value) { - json_string_t *string = malloc(sizeof(json_string_t)); + json_string_t *string; + + if(!value) + return NULL; + + string = malloc(sizeof(json_string_t)); if(!string) return NULL; json_init(&string->json, JSON_STRING); string->value = strdup(value); + if(!string->value) { + free(string); + return NULL; + } + return &string->json; } json_t *json_string(const char *value) { - if(!utf8_check_string(value, -1)) + if(!value || !utf8_check_string(value, -1)) return NULL; return json_string_nocheck(value); |