Skip to content

Commit 03e5ad8

Browse files
committed
Fix potential memory leak of tev->e_list by reordering list_add before epoll_ctl
This commit addresses a potential memory leak in tgt_event_add related to tev->e_list. Previously, epoll_ctl(EPOLL_CTL_ADD) was called before list_add. This sequence could lead to a race condition where the epoll event is registered and immediately triggered, leading to EPOLL_CTL_DEL and list_del being called before list_add, resulting in the event being removed from epoll but never added to the list, causing a memory leak after calling list_add (Because no one will call list_del to remove this tev->e_list anymore). To prevent this, list_add is now called before epoll_ctl(EPOLL_CTL_ADD). This ensures that the tev->e_list is added to the list before any epoll events can be triggered, allowing proper cleanup by list_del in case of an early EPOLL_CTL_DEL. Signed-off-by: JK Chen <[email protected]>
1 parent 0984d21 commit 03e5ad8

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

usr/tgtd.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,13 @@ int tgt_event_add(int fd, int events, event_handler_t handler, void *data)
217217
memset(&ev, 0, sizeof(ev));
218218
ev.events = events;
219219
ev.data.ptr = tev;
220+
list_add(&tev->e_list, &tgt_events_list);
220221
err = epoll_ctl(ep_fd, EPOLL_CTL_ADD, fd, &ev);
221222
if (err) {
222223
eprintf("Cannot add fd, %m\n");
224+
list_del(&tev->e_list);
223225
free(tev);
224-
} else
225-
list_add(&tev->e_list, &tgt_events_list);
226+
}
226227

227228
return err;
228229
}

0 commit comments

Comments
 (0)