Make bt_uuid_from_str() take begin/end pointers
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 10 May 2024 19:11:15 +0000 (15:11 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 4 Sep 2024 19:05:14 +0000 (15:05 -0400)
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 <eeppeliteloop@gmail.com>
Change-Id: I4d7c0c434b71c684a90fa696c6654e4b3a5c055c
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12758

src/common/uuid.c
src/common/uuid.h
src/cpp-common/bt2c/uuid.hpp
src/plugins/ctf/common/src/metadata/tsdl/ast.hpp
tests/lib/test-bt-uuid.c

index 5c709f73e2eca890550b5f8666327323fbdad3bf..7120ccf119f87245e287d997e3084c63d8eca0f5 100644 (file)
@@ -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;
        }
 
index 1c656a94667e30c655e9bedadb80deff5fb3389b..b3ba0772fbfd338843343dc1b9786df265369694 100644 (file)
@@ -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);
 
index 3bc1d7a492869d8db4c77e2d4b5dbd6471ca4b12..4b5156ecbd1be8f15e9c3fc6fa58dbf6d4664056 100644 (file)
@@ -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);
     }
 
index d17db5acccd2cf4f93b69033fe921d11bff4d1ff..25efe88c2ed3dfaa4289c766a4782dbeb9ae45ad 100644 (file)
@@ -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;
index 0c881ba346b7682c3a88218b9d122b9cb7deb3e2..e0b018af681c1f12dc741cc2d946bca33138dea0 100644 (file)
@@ -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();
This page took 0.028688 seconds and 4 git commands to generate.