# HG changeset patch # User Rob Landley # Date 1420365052 21600 # Node ID 8e506e89bb8c0c8d7b465d6d7dfffb2b77d8a9bd # Parent 2ffee259f519a6d88b129336db2d1ecd7556d6f4 Fix sed backslash parsing in square bracket pattern sections. diff -r 2ffee259f519 -r 8e506e89bb8c toys/posix/sed.c --- a/toys/posix/sed.c Sat Jan 03 20:31:41 2015 -0600 +++ b/toys/posix/sed.c Sun Jan 04 03:50:52 2015 -0600 @@ -675,7 +675,7 @@ struct step *primal; if (!fd && *name=='-') { - error_msg("no -i on stdin"); + error_msg("-i on stdin"); return; } TT.fdout = copy_tempfile(fd, name, &tmp); @@ -699,7 +699,7 @@ // if regxex, ignore delimiter in [ranges] static char *unescape_delimited_string(char **pstr, char *delim, int regex) { - char *to, *from, d; + char *to, *from, mode = 0, d; to = from = *pstr; if (!delim || !*delim) { @@ -710,21 +710,15 @@ } else d = *delim; to = delim = xmalloc(strlen(*pstr)+1); - while (*from != d) { + while (mode || *from != d) { if (!*from) return 0; // delimiter in regex character range doesn't count if (*from == '[') { - int len = 1; - - if (from[len] == ']') len++; - while (from[len] != ']') if (!from[len++]) return 0; - memmove(to, from, ++len); - to += len; - from += len; - continue; - } - if (*from == '\\') { + mode = '['; + if (from[1] == ']') *(to++) = *(from++); + } else if (mode && *from == ']') mode = 0; + else if (*from == '\\') { if (!from[1]) return 0; // Check escaped end delimiter before printf style escapes. @@ -737,7 +731,7 @@ *(to++) = c; from+=2; continue; - } + } else if (from[1]) *(to++) = *(from++); } } *(to++) = *(from++);