From 1644724a4d167a22c5ac18fb564c911995f7f07b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Koutn=C3=BD?= Date: Mon, 24 Feb 2025 15:44:29 +0100 Subject: [PATCH] sd-event: Fix sd_event_source leak Hinted by CID#1591563 but the issue is different -- when sd_event_source_set_destroy_callback() fails, we would use the old n_sources value and possibly missing sd_event_source_unref() of the last added source. --- src/libsystemd/sd-event/event-util.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libsystemd/sd-event/event-util.c b/src/libsystemd/sd-event/event-util.c index 482df37225c..9e0dbd30c3b 100644 --- a/src/libsystemd/sd-event/event-util.c +++ b/src/libsystemd/sd-event/event-util.c @@ -274,16 +274,17 @@ int event_forward_signals( return -ENOMEM; FOREACH_ARRAY(sig, signals, n_signals) { - r = sd_event_add_signal(e, &sources[n_sources], *sig | SD_EVENT_SIGNAL_PROCMASK, event_forward_signal_callback, child); + _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; + r = sd_event_add_signal(e, &s, *sig | SD_EVENT_SIGNAL_PROCMASK, event_forward_signal_callback, child); if (r < 0) return r; - r = sd_event_source_set_destroy_callback(sources[n_sources], event_forward_signal_destroy); + r = sd_event_source_set_destroy_callback(s, event_forward_signal_destroy); if (r < 0) return r; sd_event_source_ref(child); - n_sources++; + sources[n_sources++] = TAKE_PTR(s); } *ret_sources = TAKE_PTR(sources); -- 2.47.3