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.
are APIs for querying the type, manipulating the reference count, and
for constructing and manipulating values of different types.
are APIs for querying the type, manipulating the reference count, and
for constructing and manipulating values of different types.
+Unless noted otherwise, all API functions return an error value if an
+error occurs. Depending on the function's signature, the error value
+is either *NULL* or -1. Invalid arguments or invalid input are
+apparent sources for errors. Memory allocation and I/O operations may
+also cause errors.
+
.. cfunction:: int json_typeof(const json_t *json)
Return the type of the JSON value (a :ctype:`json_type` cast to
.. cfunction:: int json_typeof(const json_t *json)
Return the type of the JSON value (a :ctype:`json_type` cast to
- :ctype:`int`). This function is actually implemented as a macro for
- speed.
+ :ctype:`int`). *json* MUST NOT be *NULL*. This function is actually
+ implemented as a macro for speed.
.. cfunction:: json_is_object(const json_t *json)
json_is_array(const json_t *json)
.. cfunction:: json_is_object(const json_t *json)
json_is_array(const json_t *json)
+ if(!key || !value)
+ return -1;
+
if(!json_is_object(json))
{
json_decref(value);
if(!json_is_object(json))
{
json_decref(value);
int json_array_set_new(json_t *json, unsigned int index, json_t *value)
{
json_array_t *array;
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);
if(!json_is_array(json))
{
json_decref(value);
int json_array_append_new(json_t *json, json_t *value)
{
json_array_t *array;
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);
if(!json_is_array(json))
{
json_decref(value);
json_t *json_string_nocheck(const char *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)
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)
{
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);
return NULL;
return json_string_nocheck(value);
if(json_array_size(array) != 0)
fail("empty array has nonzero size");
if(json_array_size(array) != 0)
fail("empty array has nonzero size");
+ if(!json_array_append(array, NULL))
+ fail("able to append NULL");
+
if(json_array_append(array, five))
fail("unable to append");
if(json_array_append(array, five))
fail("unable to append");
if(json_array_set(array, 0, seven))
fail("unable to set value");
if(json_array_set(array, 0, seven))
fail("unable to set value");
+ if(!json_array_set(array, 0, NULL))
+ fail("able to set NULL");
+
if(json_array_size(array) != 2)
fail("wrong array size");
if(json_array_size(array) != 2)
fail("wrong array size");
fail("got wrong value");
}
fail("got wrong value");
}
- json_array_set_new(array, 15, json_integer(123));
+ if(json_array_set_new(array, 15, json_integer(123)))
+ fail("unable to set new value");
+
value = json_array_get(array, 15);
if(!json_is_integer(value) || json_integer_value(value) != 123)
fail("json_array_set_new works incorrectly");
value = json_array_get(array, 15);
if(!json_is_integer(value) || json_integer_value(value) != 123)
fail("json_array_set_new works incorrectly");
- json_array_append_new(array, json_integer(321));
+ if(!json_array_set_new(array, 15, NULL))
+ fail("able to set_new NULL value");
+
+ if(json_array_append_new(array, json_integer(321)))
+ fail("unable to append new value");
+
value = json_array_get(array, json_array_size(array) - 1);
if(!json_is_integer(value) || json_integer_value(value) != 321)
fail("json_array_append_new works incorrectly");
value = json_array_get(array, json_array_size(array) - 1);
if(!json_is_integer(value) || json_integer_value(value) != 321)
fail("json_array_append_new works incorrectly");
+ if(!json_array_append_new(array, NULL))
+ fail("able to append_new NULL value");
+
json_decref(five);
json_decref(seven);
json_decref(array);
json_decref(five);
json_decref(seven);
json_decref(array);
if(json_object_set(object, "a", string))
fail("unable to set value");
if(json_object_set(object, "a", string))
fail("unable to set value");
+ if(!json_object_set(object, NULL, string))
+ fail("able to set NULL key");
+
+ if(!json_object_set(object, "a", NULL))
+ fail("able to set NULL value");
+
iter = json_object_iter(object);
if(!iter)
fail("unable to get iterator");
iter = json_object_iter(object);
if(!iter)
fail("unable to get iterator");
fail("unable to set value");
fail("unable to set value");
- json_object_set_new(object, "foo", json_integer(123));
+ if(json_object_set_new(object, "foo", json_integer(123)))
+ fail("unable to set new value");
+
value = json_object_get(object, "foo");
if(!json_is_integer(value) || json_integer_value(value) != 123)
fail("json_object_set_new works incorrectly");
value = json_object_get(object, "foo");
if(!json_is_integer(value) || json_integer_value(value) != 123)
fail("json_object_set_new works incorrectly");
+ if(!json_object_set_new(object, NULL, json_integer(432)))
+ fail("able to set_new NULL key");
+
+ if(!json_object_set_new(object, "foo", NULL))
+ fail("able to set_new NULL value");
+
json_decref(string);
json_decref(other_string);
json_decref(object);
json_decref(string);
json_decref(other_string);
json_decref(object);
fail("invalid string value");
json_decref(value);
fail("invalid string value");
json_decref(value);
+ value = json_string(NULL);
+ if(value)
+ fail("json_string(NULL) failed");
+
value = json_integer(123);
if(!value)
fail("json_integer failed");
value = json_integer(123);
if(!value)
fail("json_integer failed");