]> git.ipfire.org Git - pakfire.git/commitdiff
util: Make pakfire_string_replace more robust on invalid inputs
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Mar 2021 17:03:20 +0000 (17:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 3 Mar 2021 17:03:20 +0000 (17:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/util.c

index 70c04da6ff6db7b9553dfc743906eacec2046eb7..fe6ba5be842436a31211030b71a39fed1ab22cfc 100644 (file)
@@ -74,6 +74,16 @@ PAKFIRE_EXPORT int pakfire_string_partition(
 
 PAKFIRE_EXPORT char* pakfire_string_replace(
                const char* s, const char* pattern, const char* repl) {
+       // Return NULL on no input or no pattern
+       if (!s || !pattern) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       // Replace with nothing when repl is NULL
+       if (!repl)
+               repl = "";
+
        char* result = NULL;
        const char** cache = NULL;
        unsigned int count = 0;