From: Philippe Proulx Date: Fri, 10 May 2024 19:11:15 +0000 (-0400) Subject: Make bt_uuid_from_str() take begin/end pointers X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=c7acd0bf7f6233ffc74ee7584990422e981093fa;p=babeltrace.git Make bt_uuid_from_str() take begin/end pointers Make bt_uuid_from_str() take begin/end pointers (null terminator not required) to make it possible to call it with a string view. What used to be bt_uuid_from_str() is now bt_uuid_from_c_str() to avoid changing existing the logic of call sites. Signed-off-by: Philippe Proulx Change-Id: I4d7c0c434b71c684a90fa696c6654e4b3a5c055c Reviewed-on: https://review.lttng.org/c/babeltrace/+/12758 --- diff --git a/src/common/uuid.c b/src/common/uuid.c index 5c709f73..7120ccf1 100644 --- a/src/common/uuid.c +++ b/src/common/uuid.c @@ -58,21 +58,35 @@ void bt_uuid_to_str(const bt_uuid_t uuid_in, char *str_out) snprintf(str_out, BT_UUID_STR_LEN + 1, BT_UUID_FMT, BT_UUID_FMT_VALUES(uuid_in)); } -int bt_uuid_from_str(const char *str_in, bt_uuid_t uuid_out) +int bt_uuid_from_c_str(const char *str, bt_uuid_t uuid_out) +{ + BT_ASSERT_DBG(str); + return bt_uuid_from_str(str, str + strlen(str), uuid_out); +} + +int bt_uuid_from_str(const char *begin, const char *end, bt_uuid_t uuid_out) { int ret = 0; bt_uuid_t uuid_scan; BT_ASSERT_DBG(uuid_out); - BT_ASSERT_DBG(str_in); + BT_ASSERT_DBG(begin); + BT_ASSERT_DBG(end); - if (strnlen(str_in, BT_UUID_STR_LEN + 1) != BT_UUID_STR_LEN) { + if (end - begin != BT_UUID_STR_LEN) { ret = -1; goto end; } - /* Scan to a temporary location in case of a partial match. */ - if (sscanf(str_in, BT_UUID_FMT, BT_UUID_SCAN_VALUES(uuid_scan)) != BT_UUID_LEN) { + /* + * Scan to a temporary location in case of a partial match. + * + * It's safe to use `begin` here even without a null terminator + * because we know the string from `begin` to `end` is large + * enough to contain a UUID string. + */ + if (sscanf(begin, BT_UUID_FMT, BT_UUID_SCAN_VALUES(uuid_scan)) != + BT_UUID_LEN) { ret = -1; } diff --git a/src/common/uuid.h b/src/common/uuid.h index 1c656a94..b3ba0772 100644 --- a/src/common/uuid.h +++ b/src/common/uuid.h @@ -39,7 +39,8 @@ typedef uint8_t bt_uuid_t[BT_UUID_LEN]; void bt_uuid_generate(bt_uuid_t uuid_out); void bt_uuid_to_str(const bt_uuid_t uuid_in, char *str_out); -int bt_uuid_from_str(const char *str_in, bt_uuid_t uuid_out); +int bt_uuid_from_c_str(const char *str, bt_uuid_t uuid_out); +int bt_uuid_from_str(const char *begin, const char *end, bt_uuid_t uuid_out); int bt_uuid_compare(const bt_uuid_t uuid_a, const bt_uuid_t uuid_b); void bt_uuid_copy(bt_uuid_t uuid_dest, const bt_uuid_t uuid_src); diff --git a/src/cpp-common/bt2c/uuid.hpp b/src/cpp-common/bt2c/uuid.hpp index 3bc1d7a4..4b5156ec 100644 --- a/src/cpp-common/bt2c/uuid.hpp +++ b/src/cpp-common/bt2c/uuid.hpp @@ -137,7 +137,7 @@ public: explicit Uuid(const bt2c::CStringView str) noexcept { - const auto ret = bt_uuid_from_str(str.data(), _mUuid.data()); + const auto ret = bt_uuid_from_c_str(str.data(), _mUuid.data()); BT_ASSERT(ret == 0); } diff --git a/src/plugins/ctf/common/src/metadata/tsdl/ast.hpp b/src/plugins/ctf/common/src/metadata/tsdl/ast.hpp index d17db5ac..25efe88c 100644 --- a/src/plugins/ctf/common/src/metadata/tsdl/ast.hpp +++ b/src/plugins/ctf/common/src/metadata/tsdl/ast.hpp @@ -575,7 +575,7 @@ static inline int ctf_ast_get_unary_uuid(struct bt_list_head *head, bt_uuid_t uu } src_string = node->u.unary_expression.u.string; - ret = bt_uuid_from_str(src_string, uuid); + ret = bt_uuid_from_c_str(src_string, uuid); if (ret) { BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot parse UUID: uuid=\"{}\"", src_string); goto end; diff --git a/tests/lib/test-bt-uuid.c b/tests/lib/test-bt-uuid.c index 0c881ba3..e0b018af 100644 --- a/tests/lib/test-bt-uuid.c +++ b/tests/lib/test-bt-uuid.c @@ -40,7 +40,7 @@ static const char invalid_str_5[] = "4542ad19-9e4f-4931-8261-2101c3e089ae7"; static const char invalid_str_6[] = "XX0123"; static -void run_test_bt_uuid_from_str(void) +void run_test_bt_uuid_from_c_str(void) { int ret; bt_uuid_t uuid1; @@ -48,41 +48,41 @@ void run_test_bt_uuid_from_str(void) /* * Parse valid UUID strings, expect success. */ - ret = bt_uuid_from_str(valid_str_1, uuid1); - ok(ret == 0, "bt_uuid_from_str - Parse valid string '%s', expect success", valid_str_1); + ret = bt_uuid_from_c_str(valid_str_1, uuid1); + ok(ret == 0, "bt_uuid_from_c_str - Parse valid string '%s', expect success", valid_str_1); - ret = bt_uuid_from_str(valid_str_2, uuid1); - ok(ret == 0, "bt_uuid_from_str - Parse valid string '%s', expect success", valid_str_2); + ret = bt_uuid_from_c_str(valid_str_2, uuid1); + ok(ret == 0, "bt_uuid_from_c_str - Parse valid string '%s', expect success", valid_str_2); - ret = bt_uuid_from_str(valid_str_3, uuid1); - ok(ret == 0, "bt_uuid_from_str - Parse valid string '%s', expect success", valid_str_3); + ret = bt_uuid_from_c_str(valid_str_3, uuid1); + ok(ret == 0, "bt_uuid_from_c_str - Parse valid string '%s', expect success", valid_str_3); - ret = bt_uuid_from_str(valid_str_4, uuid1); - ok(ret == 0, "bt_uuid_from_str - Parse valid string '%s', expect success", valid_str_4); + ret = bt_uuid_from_c_str(valid_str_4, uuid1); + ok(ret == 0, "bt_uuid_from_c_str - Parse valid string '%s', expect success", valid_str_4); - ret = bt_uuid_from_str(valid_str_5, uuid1); - ok(ret == 0, "bt_uuid_from_str - Parse valid string '%s', expect success", valid_str_5); + ret = bt_uuid_from_c_str(valid_str_5, uuid1); + ok(ret == 0, "bt_uuid_from_c_str - Parse valid string '%s', expect success", valid_str_5); /* * Parse invalid UUID strings, expect failure. */ - ret = bt_uuid_from_str(invalid_str_1, uuid1); - ok(ret != 0, "bt_uuid_from_str - Parse invalid string '%s', expect failure", invalid_str_1); + ret = bt_uuid_from_c_str(invalid_str_1, uuid1); + ok(ret != 0, "bt_uuid_from_c_str - Parse invalid string '%s', expect failure", invalid_str_1); - ret = bt_uuid_from_str(invalid_str_2, uuid1); - ok(ret != 0, "bt_uuid_from_str - Parse invalid string '%s', expect failure", invalid_str_2); + ret = bt_uuid_from_c_str(invalid_str_2, uuid1); + ok(ret != 0, "bt_uuid_from_c_str - Parse invalid string '%s', expect failure", invalid_str_2); - ret = bt_uuid_from_str(invalid_str_3, uuid1); - ok(ret != 0, "bt_uuid_from_str - Parse invalid string '%s', expect failure", invalid_str_3); + ret = bt_uuid_from_c_str(invalid_str_3, uuid1); + ok(ret != 0, "bt_uuid_from_c_str - Parse invalid string '%s', expect failure", invalid_str_3); - ret = bt_uuid_from_str(invalid_str_4, uuid1); - ok(ret != 0, "bt_uuid_from_str - Parse invalid string '%s', expect failure", invalid_str_4); + ret = bt_uuid_from_c_str(invalid_str_4, uuid1); + ok(ret != 0, "bt_uuid_from_c_str - Parse invalid string '%s', expect failure", invalid_str_4); - ret = bt_uuid_from_str(invalid_str_5, uuid1); - ok(ret != 0, "bt_uuid_from_str - Parse invalid string '%s', expect failure", invalid_str_5); + ret = bt_uuid_from_c_str(invalid_str_5, uuid1); + ok(ret != 0, "bt_uuid_from_c_str - Parse invalid string '%s', expect failure", invalid_str_5); - ret = bt_uuid_from_str(invalid_str_6, uuid1); - ok(ret != 0, "bt_uuid_from_str - Parse invalid string '%s', expect failure", invalid_str_6); + ret = bt_uuid_from_c_str(invalid_str_6, uuid1); + ok(ret != 0, "bt_uuid_from_c_str - Parse invalid string '%s', expect failure", invalid_str_6); } static @@ -106,12 +106,12 @@ void run_test_bt_uuid_compare(void) int ret; bt_uuid_t uuid1, uuid2; - bt_uuid_from_str(valid_str_1, uuid1); - bt_uuid_from_str(valid_str_1, uuid2); + bt_uuid_from_c_str(valid_str_1, uuid1); + bt_uuid_from_c_str(valid_str_1, uuid2); ret = bt_uuid_compare(uuid1, uuid2); ok(ret == 0, "bt_uuid_compare - Compare same UUID, expect success"); - bt_uuid_from_str(valid_str_2, uuid2); + bt_uuid_from_c_str(valid_str_2, uuid2); ret = bt_uuid_compare(uuid1, uuid2); ok(ret != 0, "bt_uuid_compare - Compare different UUID, expect failure"); ok(ret < 0, "bt_uuid_compare - Compare different UUID, expect uuid1 smaller"); @@ -166,7 +166,7 @@ void run_test(void) { plan_tests(NR_TESTS); - run_test_bt_uuid_from_str(); + run_test_bt_uuid_from_c_str(); run_test_bt_uuid_to_str(); run_test_bt_uuid_compare(); run_test_bt_uuid_copy();