Files
libuev/test/error.c
T
Joachim Wiberg 0b5461d5ff Fix NULL pointer dereference in uev_timer_init()
uev_timer_init() crashes when called without a watcher:

    uev_timer_init(ctx, NULL, cb, NULL, 100, 0);

The cleanup after a failed setup resets the descriptor in the watcher
without checking that there is one.

Check the arguments first, like the other watcher types do.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-16 15:08:43 +01:00

69 lines
1.3 KiB
C

/* Verifies cleanup on failure to set up a context or watcher */
#include "check.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);
}
/* 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_exit(&ctx) == 0);
}
int main(void)
{
event_init();
null_init();
ctx_init();
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/