From: Michael Tremer Date: Sat, 11 Jan 2025 13:40:55 +0000 (+0000) Subject: parser: Use existing string functions for copying strings X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=20ee488fd415f21bf3aaa7894178c7a3bdb682b5;p=people%2Fric9%2Fpakfire.git parser: Use existing string functions for copying strings Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/parser/scanner.l b/src/pakfire/parser/scanner.l index fa78fabff..f90b5b525 100644 --- a/src/pakfire/parser/scanner.l +++ b/src/pakfire/parser/scanner.l @@ -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