diff --git a/src/uev.c b/src/uev.c index ca757bc..b75cdc4 100644 --- a/src/uev.c +++ b/src/uev.c @@ -226,6 +226,7 @@ int uev_init1(uev_ctx_t *ctx, int maxevents) maxevents = UEV_MAX_EVENTS; memset(ctx, 0, sizeof(*ctx)); + ctx->fd = -1; ctx->maxevents = maxevents; return _init(ctx, 0); diff --git a/test/error.c b/test/error.c index 3c9dc34..f378b91 100644 --- a/test/error.c +++ b/test/error.c @@ -1,5 +1,6 @@ /* Verifies cleanup on failure to set up a context or watcher */ #include "check.h" +#include /* A failed uev_event_init() must not leak its eventfd */ static void event_init(void) @@ -21,9 +22,29 @@ static void event_init(void) 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); +} + int main(void) { event_init(); + ctx_init(); return 0; }