X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fctf-ir%2Fattributes.c;h=ccd93b17a9634d31801a00cd212a2ad3acc75986;hb=7b33a0e0d8f23d90285ea7c7820a725bcbd96c6b;hp=f7e03a86cf9e89dd54bebb8fc9b258ebb2126f82;hpb=32e87cebe6d26aefcff30cf542fb11f33a3ed7ea;p=babeltrace.git diff --git a/lib/ctf-ir/attributes.c b/lib/ctf-ir/attributes.c index f7e03a86..ccd93b17 100644 --- a/lib/ctf-ir/attributes.c +++ b/lib/ctf-ir/attributes.c @@ -28,16 +28,19 @@ #define BT_LOG_TAG "ATTRS" #include +#include #include #include +#include #include -#include +#include +#include -#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; @@ -68,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; @@ -94,19 +97,26 @@ const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj, goto end; } - attr_field_obj = bt_value_array_get(attr_obj, index); + 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_borrow(attr_obj, index); if (!attr_field_obj) { BT_LOGE("Cannot get attributes object's array value's element by index: " "value-addr=%p, index=%" PRIu64, attr_obj, index); goto end; } - attr_field_name_obj = bt_value_array_get(attr_field_obj, - BT_CTF_ATTR_NAME_INDEX); + attr_field_name_obj = bt_value_array_borrow(attr_field_obj, + 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; } @@ -118,13 +128,11 @@ const char *bt_ctf_attributes_get_field_name(struct bt_value *attr_obj, } end: - BT_PUT(attr_field_name_obj); - BT_PUT(attr_field_obj); return ret; } BT_HIDDEN -struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj, +struct bt_value *bt_attributes_borrow_field_value(struct bt_value *attr_obj, uint64_t index) { struct bt_value *value_obj = NULL; @@ -135,28 +143,34 @@ struct bt_value *bt_ctf_attributes_get_field_value(struct bt_value *attr_obj, goto end; } - attr_field_obj = bt_value_array_get(attr_obj, index); + 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_borrow(attr_obj, index); if (!attr_field_obj) { BT_LOGE("Cannot get attributes object's array value's element by index: " "value-addr=%p, index=%" PRIu64, attr_obj, index); goto end; } - value_obj = bt_value_array_get(attr_field_obj, - BT_CTF_ATTR_VALUE_INDEX); + value_obj = bt_value_array_borrow(attr_field_obj, + 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: - BT_PUT(attr_field_obj); return value_obj; } static -struct bt_value *bt_ctf_attributes_get_field_by_name( +struct bt_value *bt_attributes_borrow_field_by_name( struct bt_value *attr_obj, const char *name) { uint64_t i; @@ -175,18 +189,19 @@ struct bt_value *bt_ctf_attributes_get_field_by_name( int ret; const char *field_name; - value_obj = bt_value_array_get(attr_obj, i); + value_obj = bt_value_array_borrow(attr_obj, i); if (!value_obj) { BT_LOGE("Cannot get attributes object's array value's element by index: " "value-addr=%p, index=%" PRIu64, attr_obj, i); goto error; } - attr_field_name_obj = bt_value_array_get(value_obj, 0); + attr_field_name_obj = bt_value_array_borrow(value_obj, + 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, - value_obj, (int64_t) 0); + value_obj, (int64_t) BT_ATTR_NAME_INDEX); goto error; } @@ -198,25 +213,21 @@ struct bt_value *bt_ctf_attributes_get_field_by_name( } if (!strcmp(field_name, name)) { - BT_PUT(attr_field_name_obj); break; } - BT_PUT(attr_field_name_obj); - BT_PUT(value_obj); + value_obj = NULL; } return value_obj; error: - BT_PUT(attr_field_name_obj); - BT_PUT(value_obj); - + value_obj = NULL; return value_obj; } 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; @@ -230,10 +241,11 @@ 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_borrow_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); + attr_field_obj = NULL; goto end; } @@ -260,13 +272,12 @@ int bt_ctf_attributes_set_field_value(struct bt_value *attr_obj, } end: - BT_PUT(attr_field_obj); - + bt_put(attr_field_obj); return ret; } BT_HIDDEN -struct bt_value *bt_ctf_attributes_get_field_value_by_name( +struct bt_value *bt_attributes_borrow_field_value_by_name( struct bt_value *attr_obj, const char *name) { struct bt_value *value_obj = NULL; @@ -278,29 +289,27 @@ 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_borrow_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); goto end; } - value_obj = bt_value_array_get(attr_field_obj, - BT_CTF_ATTR_VALUE_INDEX); + value_obj = bt_value_array_borrow(attr_field_obj, + 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: - BT_PUT(attr_field_obj); - return value_obj; } 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; @@ -314,7 +323,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); - assert(count >= 0); + BT_ASSERT(count >= 0); /* * We do not freeze the array value object itself here, since @@ -324,7 +333,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_borrow_field_value(attr_obj, i); if (!obj) { BT_LOGE("Cannot get attributes object's field value by index: " "value-addr=%p, index=%" PRIu64, @@ -334,7 +343,6 @@ int bt_ctf_attributes_freeze(struct bt_value *attr_obj) } bt_value_freeze(obj); - BT_PUT(obj); } end: