Commit 676cf226
676cf226dc46Merged
Ryan Seys <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
fiber: check fiber-storage allocations against OOM
sp_FiberStore_set doubled the capacity and then reused the realloc results without checking them: a failed realloc left s->cap corrupted, leaked the old buffer, and wrote through a NULL pointer on the line below. sp_FiberStore_new had the same unchecked malloc pattern.
Grow each array into a temporary and commit s->keys/s->vals/s->cap only after both succeed, so a failed realloc leaves the store intact (the original buffer survives) rather than NULL-deref'ing or double-freeing a moved buffer; raise NoMemoryError instead of crashing. Guard the initial mallocs the same way.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 676cf226dc46
- Parent
- ef0dd7102c46
lib/sp_fiber.cmodifiedView file
s->cap = 8; s->len = 0; s->keys = (sp_sym *)malloc(sizeof(sp_sym) * s->cap); s->vals = (sp_RbVal *)malloc(sizeof(sp_RbVal) * s->cap); if (!s->keys || !s->vals) { /* If one side succeeded, release it now rather than waiting for the finalizer -- and NULL both so that finalizer's free stays a no-op. */ free(s->keys); free(s->vals); s->keys = NULL; s->vals = NULL; sp_raise_cls("NoMemoryError", "failed to allocate fiber storage"); } return s;}static sp_RbVal sp_FiberStore_get(sp_FiberStore *s, sp_sym k) {static void sp_FiberStore_set(sp_FiberStore *s, sp_sym k, sp_RbVal v) { for (mrb_int i = 0; i < s->len; i++) if (s->keys[i] == k) { s->vals[i] = v; return; } if (s->len == s->cap) { s->cap *= 2; s->keys = (sp_sym *)realloc(s->keys, sizeof(sp_sym) * s->cap); s->vals = (sp_RbVal *)realloc(s->vals, sizeof(sp_RbVal) * s->cap); /* Grow each array into a temp and commit only on success, so a failed realloc leaves the store intact (the old buffer survives) instead of NULL-deref'ing on the write below or double-freeing a moved buffer. */ mrb_int nc = s->cap * 2; sp_sym *nk = (sp_sym *)realloc(s->keys, sizeof(sp_sym) * nc); if (!nk) sp_raise_cls("NoMemoryError", "failed to grow fiber storage"); s->keys = nk; sp_RbVal *nv = (sp_RbVal *)realloc(s->vals, sizeof(sp_RbVal) * nc); if (!nv) sp_raise_cls("NoMemoryError", "failed to grow fiber storage"); s->vals = nv; s->cap = nc; } s->keys[s->len] = k; s->vals[s->len] = v; s->len++;}