Files
libuev/test/error.c
T
Joachim Wiberg aedc189672 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>
2026-11-16 15:08:43 +01:00

88 lines
1.8 KiB
C

/* Verifies cleanup on failure to set up a context or watcher */
#include "check.h"
#include <signal.h>
#include <sys/resource.h>
/* A failed uev_event_init() must not leak its eventfd */
static void event_init(void)
{
uev_ctx_t ctx;
uev_t w;
int fd;
fail_unless(uev_init(&ctx) == 0);
fail_unless(close(ctx.fd) == 0);
fd = dup(STDIN_FILENO);
fail_unless(fd >= 0);
close(fd);
fail_unless(uev_event_init(&ctx, &w, NULL, NULL) != 0);
fail_unless(w.fd == -1);
fail_unless(dup(STDIN_FILENO) == fd);
close(fd);
}
/* A failed uev_init() must not leave stdin as the context descriptor */
static void ctx_init(void)
{
struct rlimit old, lim;
uev_ctx_t ctx;
int rc;
fail_unless(getrlimit(RLIMIT_NOFILE, &old) == 0);
lim = old;
lim.rlim_cur = 0;
fail_unless(setrlimit(RLIMIT_NOFILE, &lim) == 0);
rc = uev_init(&ctx);
fail_unless(setrlimit(RLIMIT_NOFILE, &old) == 0);
fail_unless(rc == -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 */
static void null_init(void)
{
uev_ctx_t ctx;
fail_unless(uev_init(&ctx) == 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);
}
int main(void)
{
event_init();
watcher_init();
null_init();
ctx_init();
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/