Skip to content

utils: Handle realloc() failure in fdlist_add() - #288

Open
Comtea04 wants to merge 1 commit into
libimobiledevice:masterfrom
Comtea04:fix-fdlist-realloc
Open

Comtea04 wants to merge 1 commit into
libimobiledevice:masterfrom
Comtea04:fix-fdlist-realloc

Conversation

@Comtea04

@Comtea04 Comtea04 commented Oct 5, 2026

Copy link
Copy Markdown

fdlist_add() doubles list->capacity and assigns the results of realloc() directly to list->owners and list->fds. When realloc() fails, the arrays are leaked and the following stores write through a NULL pointer, which crashes the daemon.

This only updates the pointers and the capacity once the reallocation succeeded. On failure it logs an error and leaves the fd out of this poll() round; the main loop rebuilds the list on every iteration, so it is picked up again next time. If only the second realloc() fails, owners is simply larger than needed, which is harmless. A capacity of 0 (after fdlist_free()) now grows to 4 instead of staying 0.

Testing: fdlist_create()/fdlist_add()/fdlist_free() copied verbatim from 3ded00c and from this branch, run under ASan+UBSan; fill the list to its capacity of 4, then make the next growth fail:

  • realloc() of owners fails: before SEGV, after the fd is skipped and the next add succeeds (count=5, fds 10,11,12,13,100)
  • realloc() of fds fails: same
  • without failure: identical result before and after (count=6 cap=8)

🤖 Generated with Claude Code

fdlist_add() assigned the results of realloc() directly to
list->owners and list->fds and doubled the capacity unconditionally,
so on out-of-memory the arrays were leaked and the following stores
wrote through a NULL pointer, crashing the daemon.

Only update the pointers and the capacity once the reallocation
succeeded. If it fails, log an error and leave the fd out of this
poll() round; the main loop rebuilds the list on every iteration.
A capacity of 0 (after fdlist_free()) now grows to 4 instead of
staying 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant