Skip to content

Commit

Permalink
Don't crash if passed NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell Harmon committed Dec 12, 2013
1 parent 7d2621d commit 08d1144
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
14 changes: 12 additions & 2 deletions util/ruminate-jansson/deserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static void *_json_deserialize( JsonState *js, RType *rt, json_t *json, bool do_
GQuark typeid = r_string_quark(name);
r_string_unref(name);

if( do_hook && js != NULL ) {
if( do_hook ) {
JsonHook *hook = g_datalist_id_get_data(&js->handlers, typeid);
if( hook != NULL && hook->deserializer != NULL ) {
return hook->deserializer((JsonDeserializerArgs){ js, rt, json, _json_deserialize_cont }, hook->deserializer_data, error);
Expand Down Expand Up @@ -278,12 +278,22 @@ static void *_json_deserialize_cont( JsonState *js, RType *rt, json_t *json, GEr
}

void *json_deserialize( JsonState *js, json_t *json, GError **error ) {
bool free_json_state = false;
if( js == NULL ) {
free_json_state = true;
js = json_state_new();
}
GPtrArray *types = ruminate_get_types_by_name(json_string_value(json_object_get(json, "type")), error);
if( types == NULL ) return false;
if( types == NULL ) goto error_types_by_name;
// TODO: Handle multiple types of this name.
//g_assert_cmpuint(types->len, ==, 1);
RType *type = g_ptr_array_index(types, 0);
void *ret = _json_deserialize(js, type, json_object_get(json, "value"), true, error);
g_ptr_array_unref(types);
if( free_json_state ) json_state_unref(js);
return ret;

error_types_by_name:
if( free_json_state ) json_state_unref(js);
return false;
}
30 changes: 20 additions & 10 deletions util/ruminate-jansson/serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ static json_t *_json_serialize( JsonState *js, RType *rt, void *value, bool do_h

json_t *ret = NULL;

if( do_hook && js != NULL ) {
if( do_hook ) {
JsonHook *hook = g_datalist_id_get_data(&js->handlers, typeid);
if( hook != NULL && hook->serializer != NULL ) {
ret = hook->serializer((JsonSerializerArgs){ js, rt, value, _json_serialize_cont }, hook->serializer_data, error);
Expand Down Expand Up @@ -349,23 +349,33 @@ static json_t *_json_serialize_cont( JsonState *js, RType *rt, void *value, GErr
}

json_t *json_serialize( JsonState *js, RType *rt, void *value, GError **error ) {
bool free_json_state = false;
if( js == NULL ) {
free_json_state = true;
js = json_state_new();
}

json_t *serialized = _json_serialize(js, rt, value, true, error);
if( serialized == NULL ) return NULL;
if( serialized == NULL ) goto error__json_serialize;

json_t *ret = serialized;
if( js->flags & JSON_FLAG_INVERTABLE ) {
RString *name = r_type_name(rt, error);
if( name == NULL ) {
json_decref(serialized);
return NULL;
}
if( name == NULL ) goto error_type_name;
// TODO: Error check
json_t *obj = json_object();
json_object_set(obj, "type", json_string(r_string_bytes(name)));
json_object_set(obj, "value", serialized);
r_string_unref(name);

return obj;
} else {
return serialized;
ret = obj;
}

if( free_json_state ) json_state_unref(js);
return ret;

error_type_name:
json_decref(serialized);
error__json_serialize:
if( free_json_state ) json_state_unref(js);
return NULL;
}

0 comments on commit 08d1144

Please sign in to comment.