From e4b41a30e4fd7da9b61837f0fc91705b53a81c06 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 21 Dec 2019 03:25:19 -0600 Subject: Tighten up unquote_file() --- toys/posix/patch.c | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'toys/posix') diff --git a/toys/posix/patch.c b/toys/posix/patch.c index 27c25143..440a90b9 100644 --- a/toys/posix/patch.c +++ b/toys/posix/patch.c @@ -246,32 +246,30 @@ done: } // read a filename that has been quoted or escaped -char *unquote_file(char *filename) { - char *s = filename, *result, *t, *u; - int quote = 0, ch; +char *unquote_file(char *filename) +{ + char *s = filename, *t; + + // Return copy of file that wasn't quoted + if (*s++ != '"' || !*s) return xstrdup(filename); // quoted and escaped filenames are larger than the original - result = xmalloc(strlen(filename) + 1); - t = result; - if (*s == '"') { - s++; - quote = 1; - } - for (; *s && !(quote && *s == '"' && !s[1]); s++) { + for (t = filename = xmalloc(strlen(s) + 1); *s != '"'; s++) { + if (!s[1]) error_exit("bad %s", filename); + // don't accept escape sequences unless the filename is quoted - if (quote && *s == '\\' && s[1]) { - if (s[1] >= '0' && s[1] < '8') { - *t++ = strtoul(s + 1, &u, 8); - s = u - 1; - } else { - ch = unescape(s[1]); - *t++ = ch ? ch : s[1]; - s++; - } - } else *t++ = *s; + if (*s != '\\') *t++ = *s; + else if (*++s >= '0' && *s < '8') { + *t++ = strtoul(s, &s, 8); + s--; + } else { + if (!(*t = unescape(*s))) *t = *s;; + t++; + } } *t = 0; - return result; + + return filename; } // Read a patch file and find hunks, opening/creating/deleting files. -- cgit v1.2.3