From: Simon Marchi Date: Sun, 8 May 2022 01:42:36 +0000 (-0400) Subject: Fix: lib: pass down API function name to some helpers X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=867eb7632255f6a84234542198bd7edaac1bce12;hp=76edbcf1a675fe2d7d058c1b3ae1bd6fbe073e1c Fix: lib: pass down API function name to some helpers Let's say I change bt_field_structure_borrow_member_field_by_index to pass an invalid index (e.g. 99999) to borrow_structure_field_member_field_by_index, to exercise the "valid index" precondition assertion, I get: (╯°□°)╯︵ ┻━┻ /home/simark/src/babeltrace/src/lib/assert-cond.c:33: format_cond_id(): Assertion `strstr(func, func_prefix) == func` failed. This is because `format_cond_id` expects to receive the name of the API function called by the user, starting with `bt_`. But it's the name of the `borrow_structure_field_member_field_by_index` helper that is passed down to `format_cond_id`, instead. Change some helpers in lib/trace-ir/field.c to pass down the API function name to their helper, to avoid this. Add some necessary "FROM_FUNC" version of assertions used by these helpers. Introducing the same "bug", I get the expected precondition assertion failure message: 05-07 21:54:06.709 2194353 2194353 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:65 Babeltrace 2 library precondition not satisfied. 05-07 21:54:06.709 2194353 2194353 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:66 ------------------------------------------------------------------------ 05-07 21:54:06.709 2194353 2194353 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:67 Condition ID: `pre:field-structure-borrow-member-field-by-index:valid-index`. 05-07 21:54:06.709 2194353 2194353 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:69 Function: bt_field_structure_borrow_member_field_by_index(). 05-07 21:54:06.709 2194353 2194353 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:70 ------------------------------------------------------------------------ 05-07 21:54:06.709 2194353 2194353 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:71 Error is: 05-07 21:54:06.709 2194353 2194353 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:73 Index is out of bounds: index=99999, count=1 05-07 21:54:06.709 2194353 2194353 F LIB/ASSERT-COND bt_lib_assert_cond_failed@assert-cond.c:76 Aborting... Change-Id: Id3c310a3c5c46358532657d38d867c5ed802df78 Reviewed-on: https://review.lttng.org/c/babeltrace/+/8002 Reviewed-by: Philippe Proulx CI-Build: Simon Marchi --- diff --git a/src/lib/assert-cond.h b/src/lib/assert-cond.h index 2c89d892..bca924e8 100644 --- a/src/lib/assert-cond.h +++ b/src/lib/assert-cond.h @@ -569,13 +569,17 @@ BT_ASSERT_PRE_DEV_NON_NULL(_BT_ASSERT_PRE_FP_ID, (_fp), \ _BT_ASSERT_PRE_FP_NAME) -#define BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(_field_id, _field, _cls_type_id, _cls_type, _name) \ - BT_ASSERT_PRE_DEV("is-" _cls_type_id ":" _field_id, \ +#define BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE_FROM_FUNC(_func, _field_id, _field, _cls_type_id, _cls_type, _name) \ + BT_ASSERT_PRE_DEV_FROM_FUNC(_func, "is-" _cls_type_id ":" _field_id, \ ((const struct bt_field *) (_field))->class->type == (_cls_type), \ _name " has the wrong class type: expected-class-type=%s, " \ "%![field-]+f", \ bt_common_field_class_type_string(_cls_type), (_field)) +#define BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE(_field_id, _field, _cls_type_id, _cls_type, _name) \ + BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE_FROM_FUNC(__func__, \ + _field_id, (_field), _cls_type_id, _cls_type, _name) + #define BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT(_field_id, _field, _name) \ BT_ASSERT_PRE_DEV( \ "is-unsigned-integer-field:" _field_id, \ @@ -592,14 +596,18 @@ _name " is not a signed integer field: %![field-]+f", \ (_field)) -#define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(_field_id, _field, _name) \ - BT_ASSERT_PRE_DEV( \ +#define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY_FROM_FUNC(_func, _field_id, _field, _name) \ + BT_ASSERT_PRE_DEV_FROM_FUNC(_func, \ "is-array-field:" _field_id, \ ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY || \ ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD || \ ((const struct bt_field *) (_field))->class->type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD, \ _name " is not an array field: %![field-]+f", (_field)) +#define BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY(_field_id, _field, _name) \ + BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY_FROM_FUNC(__func__, _field_id, \ + (_field), _name) + #define BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY(_field_id, _field, _name) \ BT_ASSERT_PRE_DEV( \ "is-dynamic-array-field:" _field_id, \ @@ -636,6 +644,11 @@ BT_ASSERT_PRE_NON_NULL(_BT_ASSERT_PRE_FIELD_ID, (_field), \ _BT_ASSERT_PRE_FIELD_NAME) +#define BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(_func, _field) \ + BT_ASSERT_PRE_DEV_NON_NULL_FROM_FUNC(_func, \ + _BT_ASSERT_PRE_FIELD_ID, (_field), \ + _BT_ASSERT_PRE_FIELD_NAME) + #define BT_ASSERT_PRE_DEV_FIELD_NON_NULL(_field) \ BT_ASSERT_PRE_DEV_NON_NULL(_BT_ASSERT_PRE_FIELD_ID, (_field), \ _BT_ASSERT_PRE_FIELD_NAME) diff --git a/src/lib/trace-ir/field.c b/src/lib/trace-ir/field.c index 9d4d1335..4d6db133 100644 --- a/src/lib/trace-ir/field.c +++ b/src/lib/trace-ir/field.c @@ -997,39 +997,45 @@ end: static inline struct bt_field *borrow_array_field_element_field_by_index( - struct bt_field *field, uint64_t index) + struct bt_field *field, uint64_t index, const char *api_func) { struct bt_field_array *array_field = (void *) field; - BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field); - BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY("field", field, "Field"); - BT_ASSERT_PRE_DEV_VALID_INDEX(index, array_field->length); + BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func, field); + BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY_FROM_FUNC(api_func, "field", field, + "Field"); + BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func, index, + array_field->length); return array_field->fields->pdata[index]; } struct bt_field *bt_field_array_borrow_element_field_by_index( struct bt_field *field, uint64_t index) { - return borrow_array_field_element_field_by_index(field, index); + return borrow_array_field_element_field_by_index(field, index, + __func__); } const struct bt_field * bt_field_array_borrow_element_field_by_index_const( const struct bt_field *field, uint64_t index) { - return borrow_array_field_element_field_by_index((void *) field, index); + return borrow_array_field_element_field_by_index((void *) field, index, + __func__); } static inline struct bt_field *borrow_structure_field_member_field_by_index( - struct bt_field *field, uint64_t index) + struct bt_field *field, uint64_t index, const char *api_func) { struct bt_field_structure *struct_field = (void *) field; - BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field); - BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field, - "structure-field", BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); - BT_ASSERT_PRE_DEV_VALID_INDEX(index, struct_field->fields->len); + BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func, field); + BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE_FROM_FUNC(api_func, "field", + field, "structure-field", BT_FIELD_CLASS_TYPE_STRUCTURE, + "Field"); + BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func, index, + struct_field->fields->len); return struct_field->fields->pdata[index]; } @@ -1037,7 +1043,7 @@ struct bt_field *bt_field_structure_borrow_member_field_by_index( struct bt_field *field, uint64_t index) { return borrow_structure_field_member_field_by_index(field, - index); + index, __func__); } const struct bt_field * @@ -1045,12 +1051,12 @@ bt_field_structure_borrow_member_field_by_index_const( const struct bt_field *field, uint64_t index) { return borrow_structure_field_member_field_by_index( - (void *) field, index); + (void *) field, index, __func__); } static inline struct bt_field *borrow_structure_field_member_field_by_name( - struct bt_field *field, const char *name) + struct bt_field *field, const char *name, const char *api_func) { struct bt_field *ret_field = NULL; struct bt_field_class_structure *struct_fc; @@ -1058,10 +1064,12 @@ struct bt_field *borrow_structure_field_member_field_by_name( gpointer orig_key; gpointer index; - BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field); - BT_ASSERT_PRE_DEV_NON_NULL("member-name", name, "Member name"); - BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field, - "structure-field", BT_FIELD_CLASS_TYPE_STRUCTURE, "Field"); + BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func, field); + BT_ASSERT_PRE_DEV_NON_NULL_FROM_FUNC(api_func, "member-name", name, + "Member name"); + BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE_FROM_FUNC(api_func, "field", + field, "structure-field", BT_FIELD_CLASS_TYPE_STRUCTURE, + "Field"); struct_fc = (void *) field->class; if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name, @@ -1079,14 +1087,15 @@ end: struct bt_field *bt_field_structure_borrow_member_field_by_name( struct bt_field *field, const char *name) { - return borrow_structure_field_member_field_by_name(field, name); + return borrow_structure_field_member_field_by_name(field, name, + __func__); } const struct bt_field *bt_field_structure_borrow_member_field_by_name_const( const struct bt_field *field, const char *name) { return borrow_structure_field_member_field_by_name( - (void *) field, name); + (void *) field, name, __func__); } void bt_field_option_set_has_field(struct bt_field *field, bt_bool has_field)