]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
parser: Use existing string functions for copying strings
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 13:40:55 +0000 (13:40 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 11 Jan 2025 13:40:55 +0000 (13:40 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/parser/scanner.l

index fa78fabff12e2d76b9ec6e501f2b714e9fd45f81..f90b5b5250c08b2ec0dcbb51aeb2161857a81533 100644 (file)
@@ -66,32 +66,28 @@ YY_DECL;
 #define unput_string(s) for (int i = strlen(s) - 1; i >= 0; i--) unput(s[i])
 
 static char* copy_string(const char* s) {
+       char* buffer = NULL;
+
+       // Return NULL on NULL input
        if (!s)
                return NULL;
 
-       // Remove any leading whitespace
-       while (*s && isspace(*s))
-               s++;
-
-       // Remove any trailing whitespace
-       const char* e = s + strlen(s);
-       while (*e && isspace(*s))
-               e--;
-
-       // Determine the length of the string
-       const size_t l = strlen(s);
+       // Copy the string
+       buffer = strdup(s);
 
-       // Allocate a working buffer
-       char* buffer = malloc(l + 1);
+       // Fail if we could not copy the string
        if (!buffer)
                return NULL;
 
-       // Copy input string to buffer
-       memcpy(buffer, s, l + 1);
+       // Strip any leading or trailing whitespace
+       pakfire_string_strip(buffer);
 
        // Pointer to the start of the string
        char* p = buffer;
 
+       // Determine the length of the string
+       const size_t l = strlen(s);
+
        char* linebreak = strstr(p, "\\\n");
        while (linebreak) {
                // Move p to the beginning of the linebreak