Fix: lttng: uninitialized pointer free'd when no sessiond is present
[lttng-tools.git] / src / common / session-consumed-size.c
index f2ae65a9e22cb2963db00c97cf58dcaa2cb47f07..dfb72632e26fed863c7463848b354655f3327498 100644 (file)
@@ -1,22 +1,13 @@
 /*
- * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License, version 2.1 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: LGPL-2.1-only
  *
- * This library is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
- * for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <lttng/condition/condition-internal.h>
 #include <lttng/condition/session-consumed-size-internal.h>
+#include <lttng/constant.h>
 #include <common/macros.h>
 #include <common/error.h>
 #include <assert.h>
@@ -58,11 +49,11 @@ bool lttng_condition_session_consumed_size_validate(
        consumed = container_of(condition, struct lttng_condition_session_consumed_size,
                        parent);
        if (!consumed->session_name) {
-               ERR("Invalid buffer condition: a target session name must be set.");
+               ERR("Invalid session consumed size condition: a target session name must be set.");
                goto end;
        }
        if (!consumed->consumed_threshold_bytes.set) {
-               ERR("Invalid session condition: a threshold must be set.");
+               ERR("Invalid session consumed size condition: a threshold must be set.");
                goto end;
        }
 
@@ -72,40 +63,45 @@ end:
 }
 
 static
-ssize_t lttng_condition_session_consumed_size_serialize(
-               const struct lttng_condition *condition, char *buf)
+int lttng_condition_session_consumed_size_serialize(
+               const struct lttng_condition *condition,
+               struct lttng_dynamic_buffer *buf)
 {
-       struct lttng_condition_session_consumed_size *consumed;
-       ssize_t ret, size;
+       int ret;
        size_t session_name_len;
+       struct lttng_condition_session_consumed_size *consumed;
+       struct lttng_condition_session_consumed_size_comm consumed_comm;
 
        if (!condition || !IS_CONSUMED_SIZE_CONDITION(condition)) {
                ret = -1;
                goto end;
        }
 
-       DBG("Serializing session consumed condition");
-       consumed = container_of(condition, struct lttng_condition_session_consumed_size,
+       DBG("Serializing session consumed size condition");
+       consumed = container_of(condition,
+                       struct lttng_condition_session_consumed_size,
                        parent);
-       size = sizeof(struct lttng_condition_session_consumed_size_comm);
+
        session_name_len = strlen(consumed->session_name) + 1;
        if (session_name_len > LTTNG_NAME_MAX) {
                ret = -1;
                goto end;
        }
-       size += session_name_len;
-       if (buf) {
-               struct lttng_condition_session_consumed_size_comm consumed_comm = {
-                       .consumed_threshold_bytes = consumed->consumed_threshold_bytes.value,
-                       .session_name_len = session_name_len,
-               };
 
-               memcpy(buf, &consumed_comm, sizeof(consumed_comm));
-               buf += sizeof(consumed_comm);
-               memcpy(buf, consumed->session_name, session_name_len);
-               buf += session_name_len;
+       consumed_comm.consumed_threshold_bytes =
+                       consumed->consumed_threshold_bytes.value;
+       consumed_comm.session_name_len = (uint32_t) session_name_len;
+
+       ret = lttng_dynamic_buffer_append(buf, &consumed_comm,
+                       sizeof(consumed_comm));
+       if (ret) {
+               goto end;
+       }
+       ret = lttng_dynamic_buffer_append(buf, consumed->session_name,
+                       session_name_len);
+       if (ret) {
+               goto end;
        }
-       ret = size;
 end:
        return ret;
 }
@@ -130,8 +126,9 @@ bool lttng_condition_session_consumed_size_is_equal(const struct lttng_condition
                }
        }
 
-       if ((a->session_name && !b->session_name) ||
-                       (!a->session_name && b->session_name)) {
+       assert(a->session_name);
+       assert(b->session_name);
+       if (strcmp(a->session_name, b->session_name)) {
                goto end;
        }
 
@@ -192,7 +189,7 @@ ssize_t init_condition_from_buffer(struct lttng_condition *condition,
        status = lttng_condition_session_consumed_size_set_threshold(condition,
                        condition_comm->consumed_threshold_bytes);
        if (status != LTTNG_CONDITION_STATUS_OK) {
-               ERR("Failed to initialize session consumed condition threshold");
+               ERR("Failed to initialize session consumed size condition threshold");
                ret = -1;
                goto end;
        }
@@ -207,7 +204,7 @@ ssize_t init_condition_from_buffer(struct lttng_condition *condition,
        status = lttng_condition_session_consumed_size_set_session_name(condition,
                        session_name);
        if (status != LTTNG_CONDITION_STATUS_OK) {
-               ERR("Failed to set buffer consumed session name");
+               ERR("Failed to set session consumed size condition's session name");
                ret = -1;
                goto end;
        }
@@ -252,7 +249,6 @@ error:
 
 static
 struct lttng_evaluation *create_evaluation_from_buffer(
-               enum lttng_condition_type type,
                const struct lttng_buffer_view *view)
 {
        const struct lttng_evaluation_session_consumed_size_comm *comm =
@@ -263,7 +259,7 @@ struct lttng_evaluation *create_evaluation_from_buffer(
                goto end;
        }
 
-       evaluation = lttng_evaluation_session_consumed_size_create(type,
+       evaluation = lttng_evaluation_session_consumed_size_create(
                        comm->session_consumed);
 end:
        return evaluation;
@@ -282,8 +278,7 @@ ssize_t lttng_evaluation_session_consumed_size_create_from_buffer(
                goto error;
        }
 
-       evaluation = create_evaluation_from_buffer(
-                       LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE, view);
+       evaluation = create_evaluation_from_buffer(view);
        if (!evaluation) {
                ret = -1;
                goto error;
@@ -396,24 +391,17 @@ end:
 }
 
 static
-ssize_t lttng_evaluation_session_consumed_size_serialize(
-               struct lttng_evaluation *evaluation, char *buf)
+int lttng_evaluation_session_consumed_size_serialize(
+               const struct lttng_evaluation *evaluation,
+               struct lttng_dynamic_buffer *buf)
 {
-       ssize_t ret;
        struct lttng_evaluation_session_consumed_size *consumed;
+       struct lttng_evaluation_session_consumed_size_comm comm;
 
        consumed = container_of(evaluation, struct lttng_evaluation_session_consumed_size,
                        parent);
-       if (buf) {
-               struct lttng_evaluation_session_consumed_size_comm comm = {
-                       .session_consumed = consumed->session_consumed,
-               };
-
-               memcpy(buf, &comm, sizeof(comm));
-       }
-
-       ret = sizeof(struct lttng_evaluation_session_consumed_size_comm);
-       return ret;
+       comm.session_consumed = consumed->session_consumed;
+       return lttng_dynamic_buffer_append(buf, &comm, sizeof(comm));
 }
 
 static
@@ -429,7 +417,7 @@ void lttng_evaluation_session_consumed_size_destroy(
 
 LTTNG_HIDDEN
 struct lttng_evaluation *lttng_evaluation_session_consumed_size_create(
-               enum lttng_condition_type type, uint64_t consumed)
+               uint64_t consumed)
 {
        struct lttng_evaluation_session_consumed_size *consumed_eval;
 
@@ -438,7 +426,7 @@ struct lttng_evaluation *lttng_evaluation_session_consumed_size_create(
                goto end;
        }
 
-       consumed_eval->parent.type = type;
+       consumed_eval->parent.type = LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE;
        consumed_eval->session_consumed = consumed;
        consumed_eval->parent.serialize = lttng_evaluation_session_consumed_size_serialize;
        consumed_eval->parent.destroy = lttng_evaluation_session_consumed_size_destroy;
This page took 0.026733 seconds and 5 git commands to generate.