Skip to content

Commit

Permalink
Implement noop_allocs for buf_alloc_error
Browse files Browse the repository at this point in the history
So that we don't free the statically allocated error buffer
  • Loading branch information
jhawthorn committed Apr 4, 2022
1 parent a11e8dc commit 538002b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion ext/yajl/yajl_buf.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define YAJL_BUF_INIT_SIZE 2048

Expand All @@ -46,9 +47,23 @@ struct yajl_buf_t {
yajl_alloc_funcs * alloc;
};

static void *noop_realloc(void *ctx, void *ptr, unsigned int sz) {
fprintf(stderr, "Attempt to allocate on invalid yajl_buf_t\n");
abort();
}
static void *noop_malloc(void *ctx, unsigned int sz) { return noop_realloc(ctx, NULL, sz); }
static void noop_free(void *ctx, void *ptr) { }

static yajl_alloc_funcs noop_allocs = {
.malloc = &noop_malloc,
.realloc = &noop_realloc,
.free = &noop_free,
};

// A buffer to be returned if the initial allocation fails
static struct yajl_buf_t buf_alloc_error = {
.state = yajl_buf_alloc_failed
.state = yajl_buf_alloc_failed,
.alloc = &noop_allocs
};

#include <stdio.h>
Expand Down

0 comments on commit 538002b

Please sign in to comment.