From 1573dea32bb1a3db7c35db68ed27a0f4e8b24194 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Wed, 30 Apr 2025 23:54:49 +0200 Subject: Simplify algorithm to parse filename Instead of keeping a escaped flag, we simply check again both the previous and current character for non-backlash and quote. As the filename can be empty if we don't get a maching quote, we stop there. --- src/misc.h | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/misc.h b/src/misc.h index d936720e..25f4a6f5 100644 --- a/src/misc.h +++ b/src/misc.h @@ -109,21 +109,32 @@ static inline void a_Misc_parse_content_disposition(const char *disposition, cha bool_t escaped = FALSE; const char *c; - unsigned int maxlen = strlen(s); - for (c = s; !(*c == '"' && !escaped); c++) { - if ((len = c - s + 1) > maxlen) { - return; + size_t maxlen = strlen(s); + + for (size_t i = 1; i < maxlen; i++) { + /* Find closing quote not escaped */ + if (s[i - 1] != '\\' && s[i] == '"') { + /* Copy i bytes, skip closing quote */ + len = i; + *filename = dStrndup(s, len); + break; } - escaped = *c == '\\'; } - *filename = dStrndup(s, len); } else { - for ( ; *s == '.'; ++s); + /* Ignore dots at the beginning of the filename */ + while (*s == '.') + s++; + if ((len = strcspn(s, terminators))) { *filename = dStrndup(s, len); } } + /* No filename, stop here */ + if (*filename == NULL) + return; + + /* Otherwise remove invalid characters from filename */ const char invalid_characters[] = "/\\|~"; char *f = *filename, *d = *filename; -- cgit v1.2.3