Replace assert() -> BT_ASSERT() and some preconditions with BT_ASSERT_PRE()
[babeltrace.git] / lib / ctf-ir / attributes.c
index f945799663e75a107b13ff1b853010e5e59c3901..b5fe54cd45ca61a034bd259e52ffd4c76ded11b6 100644 (file)
  * SOFTWARE.
  */
 
+#define BT_LOG_TAG "ATTRS"
+#include <babeltrace/lib-logging-internal.h>
+
+#include <babeltrace/ref.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/values.h>
+#include <babeltrace/values-internal.h>
 #include <inttypes.h>
+#include <babeltrace/compat/string-internal.h>
+#include <babeltrace/assert-internal.h>
 
-#define BT_LOG_TAG "ATTRS"
-#include <babeltrace/lib-logging-internal.h>
-
-#define BT_CTF_ATTR_NAME_INDEX         0
-#define BT_CTF_ATTR_VALUE_INDEX                1
+#define BT_ATTR_NAME_INDEX             0
+#define BT_ATTR_VALUE_INDEX            1
 
 BT_HIDDEN
-struct bt_value *bt_ctf_attributes_create(void)
+struct bt_value *bt_attributes_create(void)
 {
        struct bt_value *attr_obj;
 
@@ -67,20 +71,20 @@ struct bt_value *bt_ctf_attributes_create(void)
 }
 
 BT_HIDDEN
-void bt_ctf_attributes_destroy(struct bt_value *attr_obj)
+void bt_attributes_destroy(struct bt_value *attr_obj)
 {
        BT_LOGD("Destroying attributes object: addr=%p", attr_obj);
        bt_put(attr_obj);
 }
 
 BT_HIDDEN
-int64_t bt_ctf_attributes_get_count(struct bt_value *attr_obj)
+int64_t bt_attributes_get_count(struct bt_value *attr_obj)
 {
        return bt_value_array_size(attr_obj);
 }
 
 BT_HIDDEN
-const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj,
+const char *bt_attributes_get_field_name(struct bt_value *attr_obj,
                uint64_t index)
 {
        int rc;
@@ -93,6 +97,13 @@ const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj,
                goto end;
        }
 
+       if (index >= bt_value_array_size(attr_obj)) {
+               BT_LOGW("Invalid parameter: index is out of bounds: "
+                       "index=%" PRIu64 ", count=%" PRId64,
+                       index, bt_value_array_size(attr_obj));
+               goto end;
+       }
+
        attr_field_obj = bt_value_array_get(attr_obj, index);
        if (!attr_field_obj) {
                BT_LOGE("Cannot get attributes object's array value's element by index: "
@@ -101,11 +112,11 @@ const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj,
        }
 
        attr_field_name_obj = bt_value_array_get(attr_field_obj,
-               BT_CTF_ATTR_NAME_INDEX);
+               BT_ATTR_NAME_INDEX);
        if (!attr_field_name_obj) {
                BT_LOGE("Cannot get attribute array value's element by index: "
                        "value-addr=%p, index=%" PRIu64, attr_field_obj,
-                       (uint64_t) BT_CTF_ATTR_NAME_INDEX);
+                       (uint64_t) BT_ATTR_NAME_INDEX);
                goto end;
        }
 
@@ -123,7 +134,7 @@ end:
 }
 
 BT_HIDDEN
-struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj,
+struct bt_value *bt_attributes_get_field_value(struct bt_value *attr_obj,
                uint64_t index)
 {
        struct bt_value *value_obj = NULL;
@@ -134,6 +145,13 @@ struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj,
                goto end;
        }
 
+       if (index >= bt_value_array_size(attr_obj)) {
+               BT_LOGW("Invalid parameter: index is out of bounds: "
+                       "index=%" PRIu64 ", count=%" PRId64,
+                       index, bt_value_array_size(attr_obj));
+               goto end;
+       }
+
        attr_field_obj = bt_value_array_get(attr_obj, index);
        if (!attr_field_obj) {
                BT_LOGE("Cannot get attributes object's array value's element by index: "
@@ -142,11 +160,11 @@ struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj,
        }
 
        value_obj = bt_value_array_get(attr_field_obj,
-               BT_CTF_ATTR_VALUE_INDEX);
+               BT_ATTR_VALUE_INDEX);
        if (!value_obj) {
                BT_LOGE("Cannot get attribute array value's element by index: "
                        "value-addr=%p, index=%" PRIu64, attr_field_obj,
-                       (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
+                       (uint64_t) BT_ATTR_VALUE_INDEX);
        }
 
 end:
@@ -155,7 +173,7 @@ end:
 }
 
 static
-struct bt_value *bt_ctf_attributes_get_field_by_name(
+struct bt_value *bt_attributes_get_field_by_name(
                struct bt_value *attr_obj, const char *name)
 {
        uint64_t i;
@@ -185,7 +203,7 @@ struct bt_value *bt_ctf_attributes_get_field_by_name(
                if (!attr_field_name_obj) {
                        BT_LOGE("Cannot get attribute array value's element by index: "
                                "value-addr=%p, index=%" PRIu64,
-                               value_obj, 0);
+                               value_obj, (int64_t) 0);
                        goto error;
                }
 
@@ -215,7 +233,7 @@ error:
 }
 
 BT_HIDDEN
-int bt_ctf_attributes_set_field_value(struct bt_value *attr_obj,
+int bt_attributes_set_field_value(struct bt_value *attr_obj,
                const char *name, struct bt_value *value_obj)
 {
        int ret = 0;
@@ -229,10 +247,10 @@ int bt_ctf_attributes_set_field_value(struct bt_value *attr_obj,
                goto end;
        }
 
-       attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name);
+       attr_field_obj = bt_attributes_get_field_by_name(attr_obj, name);
        if (attr_field_obj) {
                ret = bt_value_array_set(attr_field_obj,
-                       BT_CTF_ATTR_VALUE_INDEX, value_obj);
+                       BT_ATTR_VALUE_INDEX, value_obj);
                goto end;
        }
 
@@ -265,7 +283,7 @@ end:
 }
 
 BT_HIDDEN
-struct bt_value *bt_ctf_attributes_get_field_value_by_name(
+struct bt_value *bt_attributes_get_field_value_by_name(
                struct bt_value *attr_obj, const char *name)
 {
        struct bt_value *value_obj = NULL;
@@ -277,7 +295,7 @@ struct bt_value *bt_ctf_attributes_get_field_value_by_name(
                goto end;
        }
 
-       attr_field_obj = bt_ctf_attributes_get_field_by_name(attr_obj, name);
+       attr_field_obj = bt_attributes_get_field_by_name(attr_obj, name);
        if (!attr_field_obj) {
                BT_LOGD("Cannot find attributes object's field by name: "
                        "value-addr=%p, name=\"%s\"", attr_obj, name);
@@ -285,11 +303,11 @@ struct bt_value *bt_ctf_attributes_get_field_value_by_name(
        }
 
        value_obj = bt_value_array_get(attr_field_obj,
-               BT_CTF_ATTR_VALUE_INDEX);
+               BT_ATTR_VALUE_INDEX);
        if (!value_obj) {
                BT_LOGE("Cannot get attribute array value's element by index: "
                        "value-addr=%p, index=%" PRIu64, attr_field_obj,
-                       (uint64_t) BT_CTF_ATTR_VALUE_INDEX);
+                       (uint64_t) BT_ATTR_VALUE_INDEX);
        }
 
 end:
@@ -299,7 +317,7 @@ end:
 }
 
 BT_HIDDEN
-int bt_ctf_attributes_freeze(struct bt_value *attr_obj)
+int bt_attributes_freeze(struct bt_value *attr_obj)
 {
        uint64_t i;
        int64_t count;
@@ -313,12 +331,7 @@ int bt_ctf_attributes_freeze(struct bt_value *attr_obj)
 
        BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
        count = bt_value_array_size(attr_obj);
-       if (count < 0) {
-               BT_LOGE("Cannot get array value's size: value-addr=%p",
-                       attr_obj);
-               ret = -1;
-               goto end;
-       }
+       BT_ASSERT(count >= 0);
 
        /*
         * We do not freeze the array value object itself here, since
@@ -328,7 +341,7 @@ int bt_ctf_attributes_freeze(struct bt_value *attr_obj)
        for (i = 0; i < count; ++i) {
                struct bt_value *obj = NULL;
 
-               obj = bt_ctf_attributes_get_field_value(attr_obj, i);
+               obj = bt_attributes_get_field_value(attr_obj, i);
                if (!obj) {
                        BT_LOGE("Cannot get attributes object's field value by index: "
                                "value-addr=%p, index=%" PRIu64,
This page took 0.025928 seconds and 4 git commands to generate.