]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: modernize split_pair()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 21 Dec 2024 18:34:43 +0000 (03:34 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 28 Dec 2024 07:16:27 +0000 (16:16 +0900)
- use _cleanup_free_ attribute,
- rename output arguments,
- trigger assertion when an empty separator is passed.

src/basic/string-util.c
src/basic/string-util.h
src/test/test-string-util.c

index 547580ed06725f4a23974c235f55541d8930dc38..5621ffc768fd2d1738f0fe6d558ed8a6453a2e97 100644 (file)
@@ -1043,34 +1043,26 @@ char* strrep(const char *s, unsigned n) {
         return r;
 }
 
-int split_pair(const char *s, const char *sep, char **l, char **r) {
-        char *x, *a, *b;
-
+int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second) {
         assert(s);
-        assert(sep);
-        assert(l);
-        assert(r);
-
-        if (isempty(sep))
-                return -EINVAL;
+        assert(!isempty(sep));
+        assert(ret_first);
+        assert(ret_second);
 
-        x = strstr(s, sep);
+        const char *x = strstr(s, sep);
         if (!x)
                 return -EINVAL;
 
-        a = strndup(s, x - s);
+        _cleanup_free_ char *a = strndup(s, x - s);
         if (!a)
                 return -ENOMEM;
 
-        b = strdup(x + strlen(sep));
-        if (!b) {
-                free(a);
+        _cleanup_free_ char *b = strdup(x + strlen(sep));
+        if (!b)
                 return -ENOMEM;
-        }
-
-        *l = a;
-        *r = b;
 
+        *ret_first = TAKE_PTR(a);
+        *ret_second = TAKE_PTR(b);
         return 0;
 }
 
index cc6aa183c0c6e6dde2e247038c2d14d82ac6bde8..a1592b6e6db458c765b08ebd31e10331efd2cf99 100644 (file)
@@ -214,7 +214,7 @@ char* strrep(const char *s, unsigned n);
                 _d_;                                                    \
         })
 
-int split_pair(const char *s, const char *sep, char **l, char **r);
+int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second);
 
 int free_and_strdup(char **p, const char *s);
 static inline int free_and_strdup_warn(char **p, const char *s) {
index 999d3bacb8544efb2a9045b80dcce29b80bf2f30..b692af6cc060db4a34d671a19be4bbb53cf8eabd 100644 (file)
@@ -572,21 +572,22 @@ TEST(in_charset) {
 TEST(split_pair) {
         _cleanup_free_ char *a = NULL, *b = NULL;
 
-        assert_se(split_pair("", "", &a, &b) == -EINVAL);
-        assert_se(split_pair("foo=bar", "", &a, &b) == -EINVAL);
-        assert_se(split_pair("", "=", &a, &b) == -EINVAL);
-        assert_se(split_pair("foo=bar", "=", &a, &b) >= 0);
+        ASSERT_SIGNAL(split_pair("", NULL, &a, &b), SIGABRT);
+        ASSERT_SIGNAL(split_pair("", "", &a, &b), SIGABRT);
+        ASSERT_SIGNAL(split_pair("foo=bar", "", &a, &b), SIGABRT);
+        ASSERT_SIGNAL(split_pair(NULL, "=", &a, &b), SIGABRT);
+        ASSERT_ERROR(split_pair("", "=", &a, &b), EINVAL);
+        ASSERT_OK(split_pair("foo=bar", "=", &a, &b));
         ASSERT_STREQ(a, "foo");
         ASSERT_STREQ(b, "bar");
-        free(a);
-        free(b);
-        assert_se(split_pair("==", "==", &a, &b) >= 0);
+        a = mfree(a);
+        b = mfree(b);
+        ASSERT_OK(split_pair("==", "==", &a, &b));
         ASSERT_STREQ(a, "");
         ASSERT_STREQ(b, "");
-        free(a);
-        free(b);
-
-        assert_se(split_pair("===", "==", &a, &b) >= 0);
+        a = mfree(a);
+        b = mfree(b);
+        ASSERT_OK(split_pair("===", "==", &a, &b));
         ASSERT_STREQ(a, "");
         ASSERT_STREQ(b, "=");
 }