Reset watcher descriptor on failed cron and signal init

When uev_cron_init() or uev_signal_init() fails to add the watcher to
the event loop, the watcher is left with a descriptor that is closed.
A later set or start on it then operates on that descriptor, which by
then may belong to something else.

Reset it to -1, as the timer and event watchers do.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-16 15:08:43 +01:00
1 parent 0b5461d5ff
commit aedc189672
3 files changed
+26

No files matched your search

+6
View File
@@ -68,6 +68,11 @@ int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when
{ {
int fd; int fd;
if (!w || !ctx) {
errno = EINVAL;
return -1;
}
if (when < 0 || interval < 0) { if (when < 0 || interval < 0) {
errno = ERANGE; errno = ERANGE;
return -1; return -1;
@@ -84,6 +89,7 @@ int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when
_uev_watcher_stop(w); _uev_watcher_stop(w);
exit: exit:
close(fd); close(fd);
w->fd = -1;
return -1; return -1;
} }
+1
View File
@@ -72,6 +72,7 @@ int uev_signal_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int signo
_uev_watcher_stop(w); _uev_watcher_stop(w);
exit: exit:
close(fd); close(fd);
w->fd = -1;
return -1; return -1;
} }
+19
View File
@@ -1,5 +1,6 @@
/* Verifies cleanup on failure to set up a context or watcher */ /* Verifies cleanup on failure to set up a context or watcher */
#include "check.h" #include "check.h"
#include <signal.h>
#include <sys/resource.h> #include <sys/resource.h>
/* A failed uev_event_init() must not leak its eventfd */ /* A failed uev_event_init() must not leak its eventfd */
@@ -41,6 +42,22 @@ static void ctx_init(void)
fail_unless(ctx.fd == -1); fail_unless(ctx.fd == -1);
} }
/* A failed init must not leave a closed descriptor in the watcher */
static void watcher_init(void)
{
uev_ctx_t ctx;
uev_t w;
fail_unless(uev_init(&ctx) == 0);
fail_unless(close(ctx.fd) == 0);
fail_unless(uev_cron_init(&ctx, &w, NULL, NULL, time(NULL) + 3600, 0) != 0);
fail_unless(w.fd == -1);
fail_unless(uev_signal_init(&ctx, &w, NULL, NULL, SIGUSR1) != 0);
fail_unless(w.fd == -1);
}
/* Invalid arguments must not crash */ /* Invalid arguments must not crash */
static void null_init(void) static void null_init(void)
{ {
@@ -48,12 +65,14 @@ static void null_init(void)
fail_unless(uev_init(&ctx) == 0); fail_unless(uev_init(&ctx) == 0);
fail_unless(uev_timer_init(&ctx, NULL, NULL, NULL, 100, 0) != 0); fail_unless(uev_timer_init(&ctx, NULL, NULL, NULL, 100, 0) != 0);
fail_unless(uev_cron_init(&ctx, NULL, NULL, NULL, time(NULL) + 3600, 0) != 0);
fail_unless(uev_exit(&ctx) == 0); fail_unless(uev_exit(&ctx) == 0);
} }
int main(void) int main(void)
{ {
event_init(); event_init();
watcher_init();
null_init(); null_init();
ctx_init(); ctx_init();