X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fcompat%2Fcompat-poll.c;h=32932448dfdb6816e372546788bc393ed917f924;hb=6c1c0768320135c6936c371b09731851b508c023;hp=cff9f44b8e17c369a4b015866a825044bb43ef48;hpb=d21b0d71990ac6ec4272c1f80f0ca544103628b3;p=lttng-tools.git diff --git a/src/common/compat/compat-poll.c b/src/common/compat/compat-poll.c index cff9f44b8..32932448d 100644 --- a/src/common/compat/compat-poll.c +++ b/src/common/compat/compat-poll.c @@ -16,6 +16,7 @@ */ #define _GNU_SOURCE +#define _LGPL_SOURCE #include #include #include @@ -23,6 +24,8 @@ #include #include +#include +#include #include "poll.h" @@ -40,11 +43,21 @@ static int resize_poll_event(struct compat_poll_event_array *array, assert(array); + /* Refuse to resize the array more than the max size. */ + if (new_size > poll_max_size) { + goto error; + } + ptr = realloc(array->events, new_size * sizeof(*ptr)); if (ptr == NULL) { PERROR("realloc epoll add"); goto error; } + if (new_size > array->alloc_size) { + /* Zero newly allocated memory */ + memset(ptr + array->alloc_size, 0, + (new_size - array->alloc_size) * sizeof(*ptr)); + } array->events = ptr; array->alloc_size = new_size; @@ -163,7 +176,9 @@ int compat_poll_add(struct lttng_poll_event *events, int fd, /* Check for a needed resize of the array. */ if (current->nb_fd > current->alloc_size) { /* Expand it by a power of two of the current size. */ - new_size = current->alloc_size << 1UL; + new_size = max_t(int, + 1U << utils_get_count_order_u32(current->nb_fd), + current->alloc_size << 1UL); ret = resize_poll_event(current, new_size); if (ret < 0) { goto error; @@ -200,11 +215,6 @@ int compat_poll_del(struct lttng_poll_event *events, int fd) /* Ease our life a bit. */ current = &events->current; - /* Safety check on size */ - if (new_size > poll_max_size) { - new_size = poll_max_size; - } - /* Check if we need to shrink it down. */ if ((current->nb_fd << 1UL) <= current->alloc_size && current->nb_fd >= current->init_size) { @@ -212,7 +222,9 @@ int compat_poll_del(struct lttng_poll_event *events, int fd) * Shrink if nb_fd multiplied by two is <= than the actual size and we * are above the initial size. */ - new_size = current->alloc_size >> 1UL; + new_size = max_t(int, + utils_get_count_order_u32(current->nb_fd) >> 1U, + current->alloc_size >> 1U); ret = resize_poll_event(current, new_size); if (ret < 0) { goto error;