Replace assert() -> BT_ASSERT() and some preconditions with BT_ASSERT_PRE()
[babeltrace.git] / lib / ctf-ir / attributes.c
index f3d6ba10e8806d3757035d1637f6df0e2b1a66d4..b5fe54cd45ca61a034bd259e52ffd4c76ded11b6 100644 (file)
 #include <babeltrace/ref.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/values.h>
+#include <babeltrace/values-internal.h>
 #include <inttypes.h>
-#include <assert.h>
 #include <babeltrace/compat/string-internal.h>
+#include <babeltrace/assert-internal.h>
 
 #define BT_ATTR_NAME_INDEX             0
 #define BT_ATTR_VALUE_INDEX            1
@@ -96,6 +97,13 @@ const char *bt_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: "
@@ -137,6 +145,13 @@ struct bt_value *bt_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: "
@@ -316,7 +331,7 @@ int bt_attributes_freeze(struct bt_value *attr_obj)
 
        BT_LOGD("Freezing attributes object: value-addr=%p", attr_obj);
        count = bt_value_array_size(attr_obj);
-       assert(count >= 0);
+       BT_ASSERT(count >= 0);
 
        /*
         * We do not freeze the array value object itself here, since
This page took 0.023982 seconds and 4 git commands to generate.