summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Comtea042026-10-05 11:44:56 +0900
committerGravatar Nikias Bassen2026-11-22 18:05:59 +0100
commitda392d27f2d8938bf002093bb94dc60beba6afd8 (patch)
tree36d21dc8f5246884b5dc319856e86f2adf042465 /src
parent7741349485f0741e8b2dbccd936faf487c3bdee4 (diff)
downloadusbmuxd-da392d27f2d8938bf002093bb94dc60beba6afd8.tar.gz
usbmuxd-da392d27f2d8938bf002093bb94dc60beba6afd8.tar.bz2
utils: Handle realloc() failure in fdlist_add()
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>
Diffstat (limited to 'src')
-rw-r--r--src/utils.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/utils.c b/src/utils.c
index 2cc5675..ec77234 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -50,9 +50,21 @@ void fdlist_create(struct fdlist *list)
void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events)
{
if(list->count == list->capacity) {
- list->capacity *= 2;
- list->owners = realloc(list->owners, sizeof(*list->owners) * list->capacity);
- list->fds = realloc(list->fds, sizeof(*list->fds) * list->capacity);
+ int new_capacity = list->capacity ? list->capacity * 2 : 4;
+ enum fdowner *new_owners = realloc(list->owners, sizeof(*list->owners) * new_capacity);
+ if (!new_owners) {
+ util_error("%s: out of memory, not adding fd %d", __func__, fd);
+ return;
+ }
+ list->owners = new_owners;
+ struct pollfd *new_fds = realloc(list->fds, sizeof(*list->fds) * new_capacity);
+ if (!new_fds) {
+ /* owners is already larger, which is harmless; capacity stays unchanged */
+ util_error("%s: out of memory, not adding fd %d", __func__, fd);
+ return;
+ }
+ list->fds = new_fds;
+ list->capacity = new_capacity;
}
list->owners[list->count] = owner;
list->fds[list->count].fd = fd;