From 72949d4ce836a815e4cf661a84f549924b407c24 Mon Sep 17 00:00:00 2001 From: Comtea04 Date: Mon, 5 Oct 2026 11:44:56 +0900 Subject: fls: Handle realloc() failure when growing the element list fls_parse_elements() assigned the result of realloc() directly to fls->elements, so on out-of-memory the array was leaked and the next store wrote through a NULL pointer. Grow the array before allocating the next element and stop parsing if that fails. Doing it first means there is nothing to clean up on the error path (a 0x0c element is also referenced by fls->c_element), and the elements parsed so far stay valid for fls_free(). Co-Authored-By: Claude Opus 5.5 --- src/fls.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/fls.c b/src/fls.c index 8b747a9..abf9bdb 100644 --- a/src/fls.c +++ b/src/fls.c @@ -46,6 +46,15 @@ static void fls_parse_elements(fls_file* fls) if ((offset + cur->size) > fls->size) { break; } + if ((fls->num_elements + 1) > fls->max_elements) { + fls_element** new_elements = (fls_element**)realloc(fls->elements, sizeof(fls_element*) * (fls->max_elements + 10)); + if (!new_elements) { + logger(LL_ERROR, "%s: out of memory\n", __func__); + return; + } + fls->elements = new_elements; + fls->max_elements += 10; + } fls_element* ne; switch (cur->type) { case 0x0c: @@ -88,10 +97,6 @@ static void fls_parse_elements(fls_file* fls) ne->data = (ne->size > hdrsize) ? p + hdrsize : NULL; break; } - if ((fls->num_elements + 1) > fls->max_elements) { - fls->max_elements += 10; - fls->elements = (fls_element**)realloc(fls->elements, sizeof(fls_element*) * fls->max_elements); - } fls->elements[fls->num_elements++] = ne; offset += cur->size; } while (offset < fls->size); -- cgit v1.1-32-gdbae