From dca3ca646f8f2ff460d1c71b7af8c568671c67c1 Mon Sep 17 00:00:00 2001 From: Christopher Wellons Date: Sat, 2 Feb 2019 23:18:56 -0500 Subject: [PATCH] Don't forget to double the known capacity --- endlessh.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/endlessh.c b/endlessh.c index 6f41fb6..17f66e8 100644 --- a/endlessh.c +++ b/endlessh.c @@ -97,7 +97,7 @@ pollvec_init(struct pollvec *v) { v->cap = 4; v->fill = 0; - v->fds = malloc(v->cap * sizeof(*v->fds)); + v->fds = malloc(v->cap * sizeof(v->fds[0])); if (!v->fds) { fprintf(stderr, "endlessh: pollvec initialization failed\n"); exit(EXIT_FAILURE); @@ -114,13 +114,14 @@ static int pollvec_push(struct pollvec *v, int fd, short events) { if (v->cap == v->fill) { - size_t size = v->cap * 2 * sizeof(*v->fds); - if (size < v->cap * sizeof(*v->fds)) + size_t size = v->cap * 2 * sizeof(v->fds[0]); + if (size < v->cap * sizeof(v->fds[0])) return 0; // overflow struct pollfd *grow = realloc(v->fds, size); if (!grow) return 0; v->fds = grow; + v->cap *= 2; } v->fds[v->fill].fd = fd; v->fds[v->fill].events = events;