Skip to content

Commit d7ebc7a

Browse files
author
James McLaughlin
committed
Complete implementation of json_builder_free instead of using json_value_free which doesn't free object names
1 parent 2c7104d commit d7ebc7a

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

json-builder.c

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -926,22 +926,63 @@ void json_serialize_ex (json_char * buf, json_value * value, json_serialize_opts
926926

927927
void json_builder_free (json_value * value)
928928
{
929-
if (((json_builder_value *) value)->is_builder_value)
929+
json_value * cur_value;
930+
931+
if (!value)
932+
return;
933+
934+
value->parent = 0;
935+
936+
while (value)
930937
{
931-
if (value->type == json_object)
938+
switch (value->type)
932939
{
933-
/* Names are allocated separately for builder values. In parser
934-
* values, they are part of the same allocation as the values array
935-
* itself.
936-
*/
937-
unsigned int i;
938-
939-
for (i = 0; i < value->u.object.length; ++ i)
940-
free (value->u.object.values [i].name);
941-
}
942-
}
940+
case json_array:
941+
942+
if (!value->u.array.length)
943+
{
944+
free (value->u.array.values);
945+
break;
946+
}
947+
948+
value = value->u.array.values [-- value->u.array.length];
949+
continue;
950+
951+
case json_object:
952+
953+
if (!value->u.object.length)
954+
{
955+
free (value->u.object.values);
956+
break;
957+
}
958+
959+
-- value->u.object.length;
960+
961+
if (((json_builder_value *) value)->is_builder_value)
962+
{
963+
/* Names are allocated separately for builder values. In parser
964+
* values, they are part of the same allocation as the values array
965+
* itself.
966+
*/
967+
free (value->u.object.values [value->u.object.length].name);
968+
}
943969

944-
json_value_free (value);
970+
value = value->u.object.values [value->u.object.length].value;
971+
continue;
972+
973+
case json_string:
974+
975+
free (value->u.string.ptr);
976+
break;
977+
978+
default:
979+
break;
980+
};
981+
982+
cur_value = value;
983+
value = value->parent;
984+
free (cur_value);
985+
}
945986
}
946987

947988

0 commit comments

Comments
 (0)