]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: strings: Create better return codes for all functions
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Jul 2023 11:40:44 +0000 (11:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Jul 2023 12:58:49 +0000 (12:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/string.c
tests/libpakfire/string.c

index 931ba701dde8db94cc35213ac927f2f537265319..56d5a12116073918c1b0aba7f176666b0e39d9d7 100644 (file)
@@ -45,12 +45,11 @@ int __pakfire_string_vformat(char* s, const size_t length, const char* format, v
 
        // Catch any errors
        if (required < 0)
-               return 1;
+               return -ENOBUFS;
 
        // Check if the entire string could be written
        if ((size_t)required >= length) {
-               errno = ENOMEM;
-               return 1;
+               return -ENOMEM;
        }
 
        // Success
@@ -72,30 +71,24 @@ int __pakfire_string_set(char* s, const size_t length, const char* value) {
 
 int pakfire_string_startswith(const char* s, const char* prefix) {
        // Validate input
-       if (!s || !prefix) {
-               errno = EINVAL;
-               return 1;
-       }
+       if (!s || !prefix)
+               return -EINVAL;
 
        return !strncmp(s, prefix, strlen(prefix));
 }
 
 int pakfire_string_endswith(const char* s, const char* suffix) {
        // Validate input
-       if (!s || !suffix) {
-               errno = EINVAL;
-               return 1;
-       }
+       if (!s || !suffix)
+               return -EINVAL;
 
        return !strcmp(s + strlen(s) - strlen(suffix), suffix);
 }
 
 int pakfire_string_matches(const char* s, const char* pattern) {
        // Validate input
-       if (!s || !pattern) {
-               errno = EINVAL;
-               return 1;
-       }
+       if (!s || !pattern)
+               return -EINVAL;
 
        return !!strstr(s, pattern);
 }
index cfc0214022e213c11d5aed154235a4ac44b6fbd8..f584e60156e714b48588ac4d1aa767a8e3e4d52e 100644 (file)
@@ -35,7 +35,7 @@ static int test_string_set(const struct test* t) {
        ASSERT_STRING_EQUALS(buffer, "ABC");
 
        // Write a string which would not fit
-       ASSERT_ERRNO(pakfire_string_set(buffer, "1234"), ENOMEM);
+       ASSERT(pakfire_string_set(buffer, "1234") == -ENOMEM);
        ASSERT_STRING_EQUALS(buffer, "123");
 
        return EXIT_SUCCESS;
@@ -49,9 +49,9 @@ static int test_string_startswith(const struct test* t) {
        ASSERT_FALSE(pakfire_string_startswith("ABC", "B"));
 
        // Check for invalid inputs
-       ASSERT_ERRNO(pakfire_string_startswith("ABC", NULL), EINVAL);
-       ASSERT_ERRNO(pakfire_string_startswith(NULL, "ABC"), EINVAL);
-       ASSERT_ERRNO(pakfire_string_startswith(NULL, NULL), EINVAL);
+       ASSERT(pakfire_string_startswith("ABC", NULL) == -EINVAL);
+       ASSERT(pakfire_string_startswith(NULL, "ABC") == -EINVAL);
+       ASSERT(pakfire_string_startswith(NULL, NULL) == -EINVAL);
 
        return EXIT_SUCCESS;
 
@@ -64,9 +64,9 @@ static int test_string_endswith(const struct test* t) {
        ASSERT_FALSE(pakfire_string_endswith("ABC", "B"));
 
        // Check for invalid inputs
-       ASSERT_ERRNO(pakfire_string_endswith("ABC", NULL), EINVAL);
-       ASSERT_ERRNO(pakfire_string_endswith(NULL, "ABC"), EINVAL);
-       ASSERT_ERRNO(pakfire_string_endswith(NULL, NULL), EINVAL);
+       ASSERT(pakfire_string_endswith("ABC", NULL) == -EINVAL);
+       ASSERT(pakfire_string_endswith(NULL, "ABC") == -EINVAL);
+       ASSERT(pakfire_string_endswith(NULL, NULL) == -EINVAL);
 
        return EXIT_SUCCESS;
 
@@ -85,9 +85,9 @@ static int test_string_matches(const struct test* t) {
        ASSERT_FALSE(pakfire_string_matches("ABC", "ABCD"));
 
        // Check for invalid inputs
-       ASSERT_ERRNO(pakfire_string_matches("ABC", NULL), EINVAL);
-       ASSERT_ERRNO(pakfire_string_matches(NULL, "ABC"), EINVAL);
-       ASSERT_ERRNO(pakfire_string_matches(NULL, NULL), EINVAL);
+       ASSERT(pakfire_string_matches("ABC", NULL) == -EINVAL);
+       ASSERT(pakfire_string_matches(NULL, "ABC") == -EINVAL);
+       ASSERT(pakfire_string_matches(NULL, NULL) == -EINVAL);
 
        return EXIT_SUCCESS;
 
@@ -282,7 +282,7 @@ static int test_format_size(const struct test* t) {
        ASSERT_SUCCESS(pakfire_format_size(buffer, 1024 * 1024) );
        ASSERT_STRING_EQUALS(buffer, "1.0M");
 
-       ASSERT_ERRNO(pakfire_format_size(small_buffer, 0), ENOMEM);
+       ASSERT(pakfire_format_size(small_buffer, 0) == -ENOMEM);
 
        return EXIT_SUCCESS;