]> git.ipfire.org Git - pakfire.git/commitdiff
path: Make the static analyzer happy
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 22 Oct 2024 15:37:07 +0000 (15:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 22 Oct 2024 15:37:07 +0000 (15:37 +0000)
I don't see how path can be NULL here, but let's rather be safe and
check for it.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/path.c

index a0262ebde8fd5067c64bc5cfc8e9227bc0d229cf..e18bf8d96e000635d3db3b21589777f2631e721e 100644 (file)
@@ -38,7 +38,6 @@ struct pakfire_path {
 static void pakfire_path_free_segments(struct pakfire_path* path) {
        for (unsigned int i = 0; i < path->num_segments; i++)
                free(path->segments[i]);
-
        free(path->segments);
 
        // Reset the data structure
@@ -128,7 +127,7 @@ static int pakfire_path_import_segments(struct pakfire_path* path, const char* s
 
        // Copy path into buffer
        r = pakfire_string_set(buffer, s);
-       if (r)
+       if (r < 0)
                return r;
 
        const char* segment = strtok_r(buffer, "/", &p);
@@ -136,7 +135,7 @@ static int pakfire_path_import_segments(struct pakfire_path* path, const char* s
        // Append all segments
        while (segment) {
                r = pakfire_path_append_segment(path, segment);
-               if (r)
+               if (r < 0)
                        break;
 
                segment = strtok_r(NULL, "/", &p);
@@ -160,11 +159,12 @@ static int pakfire_path_parse(struct pakfire_path** path, const char* s) {
 
        // Import the path
        r = pakfire_path_import_segments(p, s);
-       if (r)
+       if (r < 0)
                goto ERROR;
 
        // Success
        *path = p;
+
        return 0;
 
 ERROR:
@@ -222,6 +222,10 @@ static int pakfire_path_to_string(struct pakfire_path* path, char* buffer, const
 static int pakfire_path_do_normalize(struct pakfire_path* path) {
        int r;
 
+       // Make the analyzer happy
+       if (!path)
+               return -EINVAL;
+
        for (unsigned int i = 0; i < path->num_segments; i++) {
                // Simply remove single dots
                if (strcmp(path->segments[i], ".") == 0) {
@@ -375,7 +379,11 @@ int __pakfire_path_basename(char* buffer, const size_t length, const char* s) {
 
        // Parse the path
        r = pakfire_path_parse(&path, s);
-       if (r)
+       if (r < 0)
+               goto ERROR;
+
+       // Make the static analyzer happy
+       if (!path)
                goto ERROR;
 
        // Drop everything but the last segment
@@ -413,6 +421,10 @@ int __pakfire_path_dirname(char* buffer, const size_t length, const char* s) {
        if (r)
                goto ERROR;
 
+       // Make the static analyzer happy
+       if (!path)
+               goto ERROR;
+
        // Drop the last segment
        r = pakfire_path_remove_segment(path, path->num_segments - 1);
        if (r)
@@ -445,6 +457,10 @@ int __pakfire_path_relative(char* buffer, const size_t length, const char* __roo
        if (r)
                goto ERROR;
 
+       // Make the static analyzer happy
+       if (!root || !path)
+               goto ERROR;
+
        // Both paths must be absolute
        if (!root->is_absolute || !path->is_absolute) {
                r = -EINVAL;