sed: fix \[cdox] at end of input

* sed/compile.c: Include minmax.h.  Do not include ctype.h.
(convert_number): Examine starting at BUF, not at BUF + 1; caller
changed.  Use a clearer way to stop the loop, which involves
addition not multiplication.  Always store the resulting integer,
not a byte taken possibly from after end of input.
(normalize_text): Do not go past end of input.  Treat incomplete
\c, \d, \o, and \x escapes like other unrecognized escapes.
Use c_toupper, not toupper, to avoid glitches in Turkish locales.
* testsuite/normalize-text.sh:
Test the fixed behavior, removing a TODO.
* testsuite/recursive-escape-c.sh: Adjust to new behavior.
This commit is contained in:
Paul Eggert committed 2026-08-17 01:17:23 -07:00
1 parent 8b7aa3d6cd
commit 1e096959ac
5 files changed
+68 -59

No files matched your search

+10
View File
@@ -20,6 +20,16 @@ GNU sed NEWS -*- outline -*-
The quoting style is the same as 'ls --quoting-style=shell-escape'.
[bug present since the beginning]
Any '\c', '\d', '\o', or '\x' at the end of a string that can hold
escapes is now treated like other unrecognized escapes. Formerly
the behavior was inconsistent and in rare cases undefined. In any
case the behavior is undocumented so users should not rely on it.
[bug present since the beginning]
The escape '\ci' now means control-I (U+0009 TAB) even in a
tr_TR.iso88599 locale where capitalizing 'i' does not yield 'I'.
[bug present since the beginning]
* Noteworthy changes in release 4.10 (2026-04-21) [stable]
+15 -6
View File
@@ -3339,21 +3339,30 @@ Produces or matches a so called ``vertical tab'' (@sc{ascii} 11).
@item \c@var{x}
Produces or matches @kbd{@sc{Control}-@var{x}}, where @var{x} is
any character. The precise effect of @samp{\c@var{x}} is as follows:
if @var{x} is a lower case letter, it is converted to upper case.
Then bit 6 of the character (hex 40) is inverted. Thus @samp{\cz} becomes
any single byte. The precise effect of @samp{\c@var{x}} is as follows:
if @var{x} is a lower case ASCII letter,
it is converted to upper case ASCII using English-language rules.
Then bit 6 of the byte (hex 40) is inverted. Thus @samp{\cz} becomes
hex 1A, but @samp{\c@{} becomes hex 3B, while @samp{\c;} becomes hex 7B.
@item \d@var{xxx}
Produces or matches a character whose decimal @sc{ascii} value is @var{xxx}.
Produces or matches a byte whose decimal value is @var{xxx}.
@item \o@var{xxx}
Produces or matches a character whose octal @sc{ascii} value is @var{xxx}.
Produces or matches a byte whose octal value is @var{xxx}.
@item \x@var{xx}
Produces or matches a character whose hexadecimal @sc{ascii} value is @var{xx}.
Produces or matches a byte whose hexadecimal value is @var{xx}.
@end table
When not followed by a digit, the @samp{\d@var{xxx}},
@samp{\o@var{xxx}}, and @samp{\x@var{xx}} escapes can contain just one
or two digits instead of the full number of digits. Only the
low-order eight bits are significant in the specified value.
Because these escapes and the @samp{\c@var{x}} escape
deal with bytes and not characters, they can produce or match
encoding-error bytes and individual bytes of multibyte characters.
@samp{\b} (backspace) was omitted because of the conflict with
the existing ``word boundary'' meaning.
+37 -38
View File
@@ -19,11 +19,11 @@
#include "sed.h"
#include <c-ctype.h>
#include <minmax.h>
#include <progname.h>
#include <read-file.h>
#include <xalloc.h>
#include <ctype.h>
#include <errno.h>
#include <obstack.h>
#include <stdckdint.h>
@@ -241,10 +241,10 @@ static char *
convert_number (char *result, char *buf, const char *bufend, int base)
{
int n = 0;
int max = 1;
char *p;
char *plim = buf + MIN (bufend - buf, 2 + (base < 16));
for (p=buf+1; p < bufend && max <= 255; ++p, max *= base)
for (p = buf; p < plim; p++)
{
int d = -1;
switch (*p)
@@ -270,10 +270,8 @@ convert_number (char *result, char *buf, const char *bufend, int base)
break;
n = n * base + d;
}
if (p == buf+1)
*result = *buf;
else
*result = n;
*result = n & UCHAR_MAX;
return p;
}
@@ -1270,8 +1268,6 @@ normalize_text (char *buf, idx_t len, enum text_types buftype)
const char *bufend = buf + len;
char *p = buf;
char *q = buf;
char ch;
int base;
/* This variable prevents normalizing text within bracket
subexpressions when conforming to POSIX. If 0, we
@@ -1292,17 +1288,20 @@ normalize_text (char *buf, idx_t len, enum text_types buftype)
continue;
}
if (*p == '\\' && p+1 < bufend && bracket_state == 0)
int base;
char ch = *p;
if (ch == '\\' && p + 1 < bufend && bracket_state == 0)
switch (*++p)
{
case 'a': *q++ = '\a'; p++; continue;
/* case 'b': *q++ = '\b'; p++; continue; --- conflicts with \b RE */
case 'f': *q++ = '\f'; p++; continue;
case 'a': ch = '\a'; break;
/* case 'b' would conflict with \b RE. */
case 'f': ch = '\f'; break;
case '\n': /*fall through */
case 'n': *q++ = '\n'; p++; continue;
case 'r': *q++ = '\r'; p++; continue;
case 't': *q++ = '\t'; p++; continue;
case 'v': *q++ = '\v'; p++; continue;
case 'n': ch = '\n'; break;
case 'r': ch = '\r'; break;
case 't': ch = '\t'; break;
case 'v': ch = '\v'; break;
case 'd': /* decimal byte */
base = 10;
@@ -1315,39 +1314,38 @@ normalize_text (char *buf, idx_t len, enum text_types buftype)
case 'o': /* octal byte */
base = 8;
convert:
p = convert_number (&ch, p, bufend, base);
if (bufend - p < 2)
goto unrecognized_escape;
char *p1 = convert_number (&ch, p + 1, bufend, base);
if (p1 == p + 1)
goto unrecognized_escape;
p = p1 - 1;
/* for an ampersand in a replacement, pass the \ up one level */
/* Re-escape any escaped & or \ in a replacement. */
if (buftype == TEXT_REPLACEMENT && (ch == '&' || ch == '\\'))
*q++ = '\\';
*q++ = ch;
continue;
break;
case 'c':
if (++p < bufend)
if (bufend - p < 2)
goto unrecognized_escape;
p++;
ch = c_toupper (*p) ^ 0x40;
if (*p == '\\')
{
*q++ = toupper ((unsigned char) *p) ^ 0x40;
if (*p == '\\')
{
p++;
if (*p != '\\')
bad_prog ("recursive escaping after \\c not allowed");
}
p++;
continue;
}
else
{
/* we just pass the \ up one level for interpretation */
if (buftype != TEXT_BUFFER)
*q++ = '\\';
continue;
if (! (p < bufend && *p == '\\'))
bad_prog ("recursive escaping after \\c not allowed");
}
break;
default:
unrecognized_escape:
/* we just pass the \ up one level for interpretation */
if (buftype != TEXT_BUFFER)
*q++ = '\\';
ch = *p;
break;
}
else if (buftype == TEXT_REGEX && posixicity != POSIXLY_EXTENDED)
@@ -1375,7 +1373,8 @@ convert:
break;
}
*q++ = *p++;
*q++ = ch;
p++;
}
return q - buf;
}
+4 -11
View File
@@ -51,7 +51,7 @@ compare_ exp2 out2 || fail=1
#
# \cX combination
#
printf "%s\n" a a a a a a a a a a > in3 || framework_failure_
printf "%s\n" a a a a a a a a a a a > in3 || framework_failure_
cat <<\EOF >prog3 || framework_failure_
1y/a/\cA/
2y/a/\ca/
@@ -63,19 +63,12 @@ cat <<\EOF >prog3 || framework_failure_
8y/a/\c[/
9y/a/\c\\/
10y/a/\c]/
11y/a/\c/
EOF
printf "\1\n\1\n\32\n\32\n;\n{\nc\n\33\n\34\n\35\n" > exp3 || framework_failure_
printf '\1\n\1\n\32\n\32\n;\n{\nc\n\33\n\34\n\35\nc\n' > exp3 \
|| framework_failure_
sed -f prog3 in3 > out3 || fail=1
compare_ exp3 out3 || fail=1
# \c at end of (valid) text - normalize_text() stops, returns control to caller.
# TODO: is this a bug?
# compare with 'y/a/\d/' and 'y/a/\x/'
cat <<\EOF >exp-err-c || framework_failure_
sed: -e expression #1, char 7: 'y' command strings have different lengths
EOF
returns_ 1 sed 'y/a/\c/' </dev/null 2>err-c || fail=1
compare_ exp-err-c err-c || fail=1
Exit $fail
+2 -4
View File
@@ -31,9 +31,7 @@ cat << \EOF > prog1 || framework_failure_
3s/./\c[/
4s/./\c]/
# '\c' at end-of-buffer, a backslash is pushed up
# on level of interpretation, and the '.' match is replaced
# with one backslash.
# '\c' at end-of-buffer is an unrecognized escape, so acts like 'c'.
5s/./\c/
# This would return incorrect results before 4.3,
@@ -42,7 +40,7 @@ cat << \EOF > prog1 || framework_failure_
EOF
# expected output:
printf '\001\n\002\n\033\n\035\n\\\n\034\n' > exp1 || framework_failure_
printf '\001\n\002\n\033\n\035\nc\n\034\n' > exp1 || framework_failure_
#
# Run simple test cases