]> git.ipfire.org Git - pakfire.git/commitdiff
string: Change that set will copy empty strings
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 31 Aug 2022 12:50:54 +0000 (12:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 31 Aug 2022 12:50:54 +0000 (12:50 +0000)
Otherwise, we would have seen "(null)" which is not what we want.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/string.h
src/libpakfire/string.c

index a0e46d9c6c1485964b9e703c06f984aee979a274..2fc1ed96e909af6a405b8f8dabfab51f0b4d8324 100644 (file)
@@ -38,7 +38,9 @@ int __pakfire_string_vformat(char* s, const size_t length,
 /*
        Simpler version when a string needs to be copied.
 */
-#define pakfire_string_set(s, value) pakfire_string_format(s, "%s", value)
+#define pakfire_string_set(s, value) \
+       __pakfire_string_set(s, sizeof(s), value)
+int __pakfire_string_set(char* s, const size_t length, const char* value);
 
 int pakfire_string_startswith(const char* s, const char* prefix);
 int pakfire_string_endswith(const char* s, const char* suffix);
index bd0b076423db7c0efebb35e4f96e6378690f04eb..c213215da9cbe501750b7821e7b0d60aa5b5ca76 100644 (file)
@@ -57,6 +57,19 @@ int __pakfire_string_vformat(char* s, const size_t length, const char* format, v
        return 0;
 }
 
+int __pakfire_string_set(char* s, const size_t length, const char* value) {
+       // If value is NULL or an empty, we will overwrite the buffer with just zeros
+       if (!value || !*value) {
+               for (unsigned int i = 0; i < length; i++)
+                       s[i] = '\0';
+
+               return 0;
+       }
+
+       // Otherwise just copy
+       return __pakfire_string_format(s, length, "%s", value);
+}
+
 int pakfire_string_startswith(const char* s, const char* prefix) {
        // Validate input
        if (!s || !prefix) {