WeaselJsonParser_reset / _destroy crash on null parser #41

Closed
opened 2026-06-29 00:11:52 +00:00 by weaselbot · 0 comments
Member

The public C API functions WeaselJsonParser_reset and WeaselJsonParser_destroy cast the supplied pointer and dispatch directly without checking for null. Passing nullptr causes immediate undefined behavior (dereferencing/invoking a destructor on a null pointer), even though WeaselJsonParser_create can legitimately return nullptr when allocation fails or the stack size is too small.

Affected code (src/lib.cpp):

__attribute__((visibility("default"))) void
WeaselJsonParser_reset(WeaselJsonParser *parser) {
  ((Parser3 *)parser)->reset();   // UB if parser == nullptr
}

__attribute__((visibility("default"))) void
WeaselJsonParser_destroy(WeaselJsonParser *parser) {
  ((Parser3 *)parser)->~Parser3(); // UB if parser == nullptr
  free(parser);
}

This is inconsistent with the usual C convention that free(NULL) is safe, and it makes cleanup code fragile when creation fails. For example:

WeaselJsonParser *p = WeaselJsonParser_create(-1, &c, nullptr, 0);
// p == nullptr
WeaselJsonParser_destroy(p); // crash / UB

Expected behavior: both functions should return early (or otherwise behave safely) when parser is nullptr, similar to how free(nullptr) is a no-op.

The public C API functions `WeaselJsonParser_reset` and `WeaselJsonParser_destroy` cast the supplied pointer and dispatch directly without checking for null. Passing `nullptr` causes immediate undefined behavior (dereferencing/invoking a destructor on a null pointer), even though `WeaselJsonParser_create` can legitimately return `nullptr` when allocation fails or the stack size is too small. Affected code (`src/lib.cpp`): ```cpp __attribute__((visibility("default"))) void WeaselJsonParser_reset(WeaselJsonParser *parser) { ((Parser3 *)parser)->reset(); // UB if parser == nullptr } __attribute__((visibility("default"))) void WeaselJsonParser_destroy(WeaselJsonParser *parser) { ((Parser3 *)parser)->~Parser3(); // UB if parser == nullptr free(parser); } ``` This is inconsistent with the usual C convention that `free(NULL)` is safe, and it makes cleanup code fragile when creation fails. For example: ```cpp WeaselJsonParser *p = WeaselJsonParser_create(-1, &c, nullptr, 0); // p == nullptr WeaselJsonParser_destroy(p); // crash / UB ``` Expected behavior: both functions should return early (or otherwise behave safely) when `parser` is `nullptr`, similar to how `free(nullptr)` is a no-op.
weaselbot was assigned by andrew 2026-06-29 17:04:46 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: weaselab/weaseljson#41