Fix: session may be NULL in relay_create_session error path
[lttng-tools.git] / src / bin / lttng-relayd / main.c
index 453bf6d20fa5625f04d76a3da2eacd87702c62db..2ffa75078779b7d0b9986696f5643267ca5ea7aa 100644 (file)
@@ -40,6 +40,7 @@
 #include <urcu/rculist.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <strings.h>
 
 #include <lttng/lttng.h>
 #include <common/common.h>
@@ -1156,21 +1157,24 @@ send_reply:
                }
        } else {
                const uint32_t output_path_length =
-                               strlen(session->output_path) + 1;
+                               session ? strlen(session->output_path) + 1 : 0;
 
                reply.output_path_length = htobe32(output_path_length);
-               ret = lttng_dynamic_buffer_append(&reply_payload, &reply,
-                               sizeof(reply));
+               ret = lttng_dynamic_buffer_append(
+                               &reply_payload, &reply, sizeof(reply));
                if (ret) {
                        ERR("Failed to append \"create session\" command reply header to payload buffer");
                        goto end;
                }
 
-               ret = lttng_dynamic_buffer_append(&reply_payload,
-                               session->output_path, output_path_length);
-               if (ret) {
-                       ERR("Failed to append \"create session\" command reply path to payload buffer");
-                       goto end;
+               if (output_path_length) {
+                       ret = lttng_dynamic_buffer_append(&reply_payload,
+                                       session->output_path,
+                                       output_path_length);
+                       if (ret) {
+                               ERR("Failed to append \"create session\" command reply path to payload buffer");
+                               goto end;
+                       }
                }
        }
 
@@ -2385,13 +2389,6 @@ static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
                }
        }
 
-       ret = session_init_output_directory_handle(
-                       conn->session, &session_output);
-       if (ret) {
-               reply_code = LTTNG_ERR_CREATE_DIR_FAIL;
-               goto end;
-       }
-
        chunk_status = lttng_trace_chunk_set_credentials_current_user(chunk);
        if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
                reply_code = LTTNG_ERR_UNK;
@@ -2399,7 +2396,14 @@ static int relay_create_trace_chunk(const struct lttcomm_relayd_hdr *recv_hdr,
                goto end;
        }
 
+       ret = session_init_output_directory_handle(
+                       conn->session, &session_output);
+       if (ret) {
+               reply_code = LTTNG_ERR_CREATE_DIR_FAIL;
+               goto end;
+       }
        chunk_status = lttng_trace_chunk_set_as_owner(chunk, &session_output);
+       lttng_directory_handle_fini(&session_output);
        if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
                reply_code = LTTNG_ERR_UNK;
                ret = -1;
@@ -2455,7 +2459,6 @@ end:
 end_no_reply:
        lttng_trace_chunk_put(chunk);
        lttng_trace_chunk_put(published_chunk);
-       lttng_directory_handle_fini(&session_output);
        return ret;
 }
 
This page took 0.024197 seconds and 4 git commands to generate.