Skip to content

Commit

Permalink
src/yajl_tree.c: support default yajl_alloc_funcs for yajl_tree_free()
Browse files Browse the repository at this point in the history
  • Loading branch information
robohack committed Jun 9, 2024
1 parent 3cfd2d8 commit b227fae
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/yajl_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,9 @@ yajl_val yajl_tree_parse (const char *input, /*+ Pointer to a null-terminated
ctx.errbuf = error_buffer;
ctx.errbuf_size = error_buffer_size;

if (error_buffer != NULL)
memset (error_buffer, 0, error_buffer_size);
if (error_buffer != NULL) {
memset(error_buffer, 0, error_buffer_size);
}

handle = yajl_alloc(&callbacks, yajl_tree_parse_afs, &ctx);
if (yajl_tree_parse_afs == NULL) {
Expand Down Expand Up @@ -564,10 +565,25 @@ void yajl_tree_free (yajl_val v) /*+ Pointer to a JSON value returned by
* "yajl_tree_parse". Passing NULL is
* valid and results in a no-op. +*/
{
static yajl_alloc_funcs afs;
bool undo_afs = false;

if (v == NULL) {
return;
}

/*
* n.b.: if the caller never pointed yajl_tree_parse_afs at their own
* yajl_alloc_funcs, then yajl_tree_parse() will have reset it to NULL on
* return and so we must set it up again, temporarily, with the default
* functions.
*/
if (yajl_tree_parse_afs == NULL) {
undo_afs = true;
yajl_set_default_alloc_funcs(&afs);
yajl_tree_parse_afs = &afs;
}

if (YAJL_IS_STRING(v)) {
if (v->u.string != NULL) {
YA_FREE(yajl_tree_parse_afs, v->u.string);
Expand All @@ -585,4 +601,8 @@ void yajl_tree_free (yajl_val v) /*+ Pointer to a JSON value returned by
} else /* if (yajl_t_true or yajl_t_false or yajl_t_null) */ {
YA_FREE(yajl_tree_parse_afs, v);
}

if (undo_afs) {
yajl_tree_parse_afs = NULL;
}
}

0 comments on commit b227fae

Please sign in to comment.