summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Comtea042026-10-05 11:44:56 +0900
committerGravatar Nikias Bassen2026-11-27 02:46:05 +0100
commit72949d4ce836a815e4cf661a84f549924b407c24 (patch)
treedc9a305e2f30050df59a21eeddff369eaa960de4
parentb7a23759cb0016d4782a44368ba775f197489f69 (diff)
downloadidevicerestore-72949d4ce836a815e4cf661a84f549924b407c24.tar.gz
idevicerestore-72949d4ce836a815e4cf661a84f549924b407c24.tar.bz2
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 <noreply@anthropic.com>
-rw-r--r--src/fls.c13
1 files 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);