Fix: miscellaneous memory handling fixes
[lttng-tools.git] / src / common / compat / compat-poll.c
index bc79eed0aaa5321c48fcd055858698bcdc5fa375..4893768a098543815207c90283eb9591d97a3a4a 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <common/defaults.h>
 #include <common/error.h>
+#include <common/macros.h>
+#include <common/utils.h>
 
 #include "poll.h"
 
@@ -50,6 +52,11 @@ static int resize_poll_event(struct compat_poll_event_array *array,
                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;
 
@@ -168,7 +175,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;
@@ -212,7 +221,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;
This page took 0.024418 seconds and 4 git commands to generate.