Closed andrei-datcu closed 2 years ago
As mentioned in the documentation: if initialization fails, yyjson_alc_pool_init() returns false and alc
is left unmodified.
Sorry for missing that.
It looks like this could cause a crash if user doesn't check the return value of yyjson_alc_pool_init().
So I modified this function: when initialization fails, use a set of placeholder functions to initialize alc
, see: https://github.com/ibireme/yyjson/commit/72f19e2d455bbfaaf41c933a8bfc74354ac61823
Just my opinion, but I disagree with this change. The user is responsible for checking the allocation worked, and that's clearly documented. I'm using my custom allocator for a reason, I don't want a different allocator suddenly involved. For example in the python wrapper this would mean we suddenly lose tracing of memory.
@TkTech This only involves allocators initialized with yyjson_alc_pool_init()
, not other custom allocators. So I think it's fine.
// A simple example:
// buffer is not big enough to create a `pool allocator`
char buf[1];
yyjson_alc alc;
yyjson_alc_pool_init(&alc, buf, sizeof(buf));
// before: crash due to 'malloc' function pointer not being set
// after: just return NULL
void *mem = alc.malloc(alc.ctx, 64);
Thanks @ibireme, I understand now :)
Describe the bug full repro
Stack:
If
BUF
's size is64
then we correctly get aNULL
document.Your environment
Additional context Reproduces on
ee9e6d5d3bde4a26b78e9cfd596dd28d231266fa