Fix: string might be uninitialized
[lttng-tools.git] / src / common / tracker.c
index afc253def71f990f77bb50923adde56bf481880e..7c97d561c982139283afde068394da7703a161c6 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2019 Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
+ * Copyright (C) 2019 Jonathan Rajotte-Julien <jonathan.rajotte-julien@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 <assert.h>
@@ -277,13 +267,13 @@ const struct lttng_tracker_id *lttng_tracker_ids_get_at_index(
        return lttng_tracker_ids_get_pointer_of_index(ids, index);
 }
 
-int lttng_tracker_ids_get_count(const struct lttng_tracker_ids *ids, unsigned int *count)
+enum lttng_tracker_id_status lttng_tracker_ids_get_count(const struct lttng_tracker_ids *ids, unsigned int *count)
 {
 
-       enum lttng_tracker_id_status status = LTTNG_ROTATION_STATUS_OK;
+       enum lttng_tracker_id_status status = LTTNG_TRACKER_ID_STATUS_OK;
 
        if (!ids || !count) {
-               status = LTTNG_ROTATION_STATUS_INVALID;
+               status = LTTNG_TRACKER_ID_STATUS_INVALID;
                goto end;
        }
 
@@ -294,13 +284,87 @@ end:
 
 void lttng_tracker_ids_destroy(struct lttng_tracker_ids *ids)
 {
+       int i;
+
        if (!ids) {
                return;
        }
 
-       for (int i = 0; i < ids->count; i++) {
+       for (i = 0; i < ids->count; i++) {
                lttng_tracker_id_reset(&ids->id_array[i]);
        }
        free(ids->id_array);
        free(ids);
 }
+
+int lttng_tracker_ids_serialize(const struct lttng_tracker_ids *ids,
+               struct lttng_dynamic_buffer *buffer)
+{
+       int ret = 0;
+       int value;
+       const char *string = NULL;
+       unsigned int count;
+       enum lttng_tracker_id_status status;
+       const struct lttng_tracker_id *id;
+       unsigned int i;
+
+       status = lttng_tracker_ids_get_count(ids, &count);
+       if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+               ret = LTTNG_ERR_INVALID;
+               goto error;
+       }
+
+       for (i = 0; i < count; i++) {
+               struct lttcomm_tracker_id_header id_hdr;
+               size_t var_data_len = 0;
+
+               id = lttng_tracker_ids_get_at_index(ids, i);
+               if (!id) {
+                       ret = -LTTNG_ERR_INVALID;
+                       goto error;
+               }
+
+               memset(&id_hdr, 0, sizeof(id_hdr));
+               id_hdr.type = lttng_tracker_id_get_type(id);
+               switch (id_hdr.type) {
+               case LTTNG_ID_ALL:
+                       break;
+               case LTTNG_ID_VALUE:
+                       status = lttng_tracker_id_get_value(id, &value);
+                       id_hdr.u.value = value;
+                       if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+                               ret = -LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+                       break;
+               case LTTNG_ID_STRING:
+                       status = lttng_tracker_id_get_string(
+                                       id, &string);
+                       if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+                               ret = -LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+
+                       id_hdr.u.var_data_len = var_data_len =
+                                       strlen(string) + 1;
+                       break;
+               default:
+                       ret = -LTTNG_ERR_INVALID;
+                       goto error;
+               }
+               ret = lttng_dynamic_buffer_append(
+                               buffer, &id_hdr, sizeof(id_hdr));
+               if (ret) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto error;
+               }
+               ret = lttng_dynamic_buffer_append(
+                               buffer, string, var_data_len);
+               if (ret) {
+                       ret = -LTTNG_ERR_NOMEM;
+                       goto error;
+               }
+       }
+error:
+       return ret;
+}
This page took 0.024688 seconds and 4 git commands to generate.