Fix: initialize temporary fd array element to -1
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Tue, 27 Apr 2021 01:07:08 +0000 (21:07 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 28 Apr 2021 21:10:57 +0000 (17:10 -0400)
These are FDs, an initialization value of zero is a valid FD number
hence we must initialize the element to -1.

This allows us to be more strict in `add_fds_to_payload` and assert that
the passed FDs are actual FDs and not only bare zero-initialized
element.

Change-Id: Icc05d98d51906636362a67588d7c42b58cf33ded
Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/common/unix.c

index 0e6c329c072c385da4e25836290f60f436ca69e5..623421cadb98642889c56b2a6cbd1f46e7a51ca4 100644 (file)
@@ -730,6 +730,8 @@ enum lttng_error_code add_fds_to_payload(struct lttng_dynamic_array *raw_fds,
                int *raw_fd = (int *) lttng_dynamic_array_get_element(
                        raw_fds, i);
 
+               assert(*raw_fd != -1);
+
                handle = fd_handle_create(*raw_fd);
                if (!handle) {
                        ret_code = LTTNG_ERR_NOMEM;
@@ -755,15 +757,19 @@ static
 ssize_t _lttcomm_recv_payload_fds_unix_sock(int sock, size_t nb_fd,
                struct lttng_payload *payload, bool blocking)
 {
+       int i = 0;
        enum lttng_error_code add_ret;
        ssize_t ret;
+       int default_value = -1;
        struct lttng_dynamic_array raw_fds;
 
        lttng_dynamic_array_init(&raw_fds, sizeof(int), close_raw_fd);
-       ret = lttng_dynamic_array_set_count(&raw_fds, nb_fd);
-       if (ret) {
-               ret = -LTTNG_ERR_NOMEM;
-               goto end;
+
+       for (i = 0; i < nb_fd; i++) {
+               if (lttng_dynamic_array_add_element(&raw_fds, &default_value)) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
        }
 
        if (blocking) {
This page took 0.026407 seconds and 4 git commands to generate.