Skip to content

Commit 8c4b18f

Browse files
author
James McLaughlin
committed
Change to @mboelstra's method of preventing zero byte allocations
1 parent 63be13a commit 8c4b18f

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

json.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ static void default_free (void * ptr, void * user_data)
8787

8888
static void * json_alloc (json_state * state, unsigned long size, int zero)
8989
{
90-
if (size == 0)
91-
size = 1;
92-
9390
if ((state->ulong_max - state->used_memory) < size)
9491
return 0;
9592

@@ -121,6 +118,9 @@ static int new_value (json_state * state,
121118
{
122119
case json_array:
123120

121+
if (value->u.array.length == 0)
122+
break;
123+
124124
if (! (value->u.array.values = (json_value **) json_alloc
125125
(state, value->u.array.length * sizeof (json_value *), 0)) )
126126
{
@@ -132,6 +132,9 @@ static int new_value (json_state * state,
132132

133133
case json_object:
134134

135+
if (value->u.object.length == 0)
136+
break;
137+
135138
values_size = sizeof (*value->u.object.values) * value->u.object.length;
136139

137140
if (! ((*(void **) &value->u.object.values) = json_alloc

0 commit comments

Comments
 (0)