From 3e7177c20c06ae622e21298a3b992e9862717608 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 26 Oct 2026 14:26:23 -0500 Subject: [PATCH] Redo unescape2() so \xFF doesn't exand to multibyte sequence. Add tests. --- lib/lib.c | 35 ++++++++++++++++++++--------------- lib/lib.h | 2 +- tests/echo.test | 16 +++++++--------- toys/pending/sh.c | 2 +- toys/posix/echo.c | 24 ++++++------------------ toys/posix/find.c | 5 +---- 6 files changed, 36 insertions(+), 48 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 2c34706e..637fbafe 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -466,26 +466,31 @@ int unescape(char c) } // parse next character advancing pointer. echo requires leading 0 in octal esc -int unescape2(char **c, int echo) +int unescape2(char *out, char **in, int echo) { - int idx = *((*c)++), i, off; + int len = 1, idx = **in, i, off = 0; + char *pp[] = {"0%3o%n"+!echo,"x%2x%n","u%4x%n","U%8x%n"}; - if (idx != '\\' || !**c) return idx; - if (**c == 'c') return 31&*(++*c); - for (i = 0; i<4; i++) { - if (sscanf(*c, (char *[]){"0%3o%n"+!echo, "x%2x%n", "u%4x%n", "U%6x%n"}[i], - &idx, &off) > 0) - { - *c += off; + if (!(*out = idx)) return 0; + ++*in; + if (idx!='\\' || !*in) return 1; + if ((idx = *((*in)++)) == 'c') *out = 31&*((*in)++); + else { + for (i = 0; i<4; i++) { + if (!sscanf(*in-1, pp[i], &idx, &off) || !off) continue; + *in += off-1; + if (i<2) *out = idx; + else len = wcrtomb(out, idx, 0); - return idx; + break; + } + if (i==4) { + if (-1!=(idx = stridx("\\abeEfnrtv'\"?0", idx))) + *out = "\\\a\b\e\e\f\n\r\t\v'\"?"[idx]; + else --*in; } } - - if (-1 == (idx = stridx("\\abeEfnrtv'\"?0", **c))) return '\\'; - ++*c; - - return "\\\a\b\e\e\f\n\r\t\v'\"?"[idx]; + return len; } // If string ends with suffix return pointer to start of suffix in string, diff --git a/lib/lib.h b/lib/lib.h index 3b6ac80d..90f6c877 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -225,7 +225,7 @@ char *strlower(char *s); char *strafter(char *haystack, char *needle); char *chomp(char *s); int unescape(char c); -int unescape2(char **c, int echo); +int unescape2(char *out, char **in, int echo); char *strend(char *str, char *suffix); int strstart(char **a, char *b); int strcasestart(char **a, char *b); diff --git a/tests/echo.test b/tests/echo.test index fef5db90..6cc246b8 100755 --- a/tests/echo.test +++ b/tests/echo.test @@ -19,29 +19,27 @@ testcmd "" "one -n" "one -n\n" "" "" testcmd "-en" "-en 'one\ntwo'" "one\ntwo" "" "" testcmd "" "--hello" "--hello\n" "" "" testcmd "-e all" "-e '\a\b\c\f\n\r\t\v\\\0123' | xxd -p" "0708\n" "" "" -testcmd "-e all but \\c" "-e '"'\a\b\f\n\r\t\v\\\0123'"' | xxd -p" \ - "07080c0a0d090b5c530a\n" "" "" +testcmd "-e all but \\c" "-e '"'\0\a\b\e\f\n\r\t\v\\\0123'"' | xxd -p" \ + "0007081b0c0a0d090b5c530a\n" "" "" +testcmd "unknown \p passes through verbatim" "-e '\p'" "\\p\n" "" "" testcmd "-nex hello" "-nex hello" "-nex hello\n" "" "" # Octal formatting tests testcmd "-e octal values" \ "-ne '\01234 \0060 \060 \0130\0131\0132 \077\012'" \ "S4 0 0 XYZ ?\n" "" "" -testcmd "-e invalid oct" "-ne 'one\\079two'" "one\a9two" "" "" -testcmd "-e \\0040" "-ne '\0040'" " " "" "" +testcmd "-e invalid oct" "-ne 'one\079two'" "one\a9two" "" "" +testcmd "-e \0040" "-ne '\0040'" " " "" "" +testcmd '-e octal >127' "-ne '\0377' | xxd -p" "ff\n" "" "" # Hexadecimal value tests testcmd "-e hexadecimal values" \ "-ne '\x534 \x30 \x58\x59\x5a \x3F\x0A'"\ "S4 0 XYZ ?\n" "" "" +testcmd "-e high hex" "-ne '\xff\xa0' | xxd -p" 'ffa0\n' '' '' testcmd "-e invalid hex 1" "-e 'one\xvdtwo'" "one\\xvdtwo\n" "" "" testcmd "-e invalid hex 2" "-e 'one\xavtwo'" "one\nvtwo\n" "" "" -# GNU extension for ESC -testcmd "-e \e" "-ne '\\e' | xxd -p" "1b\n" "" "" - -testcmd "-e \p" "-e '\\p'" "\\p\n" "" "" - # http://austingroupbugs.net/view.php?id=1222 added -E testcmd "-En" "-En 'one\ntwo'" 'one\\ntwo' "" "" testcmd "-eE" "-eE '\e'" '\\e\n' "" "" diff --git a/toys/pending/sh.c b/toys/pending/sh.c index 576939c1..bbe3618c 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -2249,7 +2249,7 @@ static int expand_arg_nobrace(struct sh_arg *arg, char *str, unsigned flags, } else if (cc == '$') { cc = *(ss = str+ii++); if (cc=='\'') { - for (s = str+ii; *s != '\''; oo += wcrtomb(new+oo, unescape2(&s, 0),0)); + for (s = str+ii; *s != '\''; oo+= unescape2(new+oo, &s, 0)); ii = s-str+1; continue; diff --git a/toys/posix/echo.c b/toys/posix/echo.c index 702efee6..51c662f3 100644 --- a/toys/posix/echo.c +++ b/toys/posix/echo.c @@ -33,27 +33,15 @@ config ECHO void echo_main(void) { - int i = 0; char *arg, *c, out[8]; + int i; - while ((arg = toys.optargs[i])) { - if (i++) putchar(' '); - - // Should we output arg verbatim? - - if (!FLAG(e)) { - xprintf("%s", arg); - continue; - } - - // Handle -e - - for (c = arg; *c; ) { - unsigned u; - + for (i = 0; (arg = toys.optargs[i]); i++) { + if (i) putchar(' '); + if (!FLAG(e)) xprintf("%s", arg); + else for (c = arg; *c; ) { if (*c == '\\' && c[1] == 'c') return; - if ((u = unescape2(&c, 1))<128) putchar(u); - else printf("%.*s", (int)wcrtomb(out, u, 0), out); + fwrite(out, unescape2(out, &c, 1), 1, stdout); } } diff --git a/toys/posix/find.c b/toys/posix/find.c index 1dcb1b83..a42d624b 100644 --- a/toys/posix/find.c +++ b/toys/posix/find.c @@ -604,11 +604,8 @@ static int do_find(struct dirtree *new) if (check) for (fmt = arg; *fmt; fmt++) { // Print the parts that aren't escapes if (*fmt == '\\') { - unsigned u; - if (fmt[1] == 'c') break; - if ((u = unescape2(&fmt, 0))<128) putchar(u); - else printf("%.*s", (int)wcrtomb(buf, u, 0), buf); + fwrite(buf, unescape2(buf, &fmt, 0), 1, stdout); fmt--; } else if (*fmt != '%') putchar(*fmt); else if (*++fmt == '%') putchar('%'); -- 2.39.5