fix: limit octal escapes in double quotes to three digits (#2725)

The scanner consumed every digit following a backslash as part of an
octal escape. A digit right after an escape was therefore swallowed:
"\0337" (ESC 7) became the single byte 0o337 instead of ESC followed by
"7", and "\0338" (ESC 8) failed to parse and became a NUL byte.

Stop after three digits, as in C and other languages with octal string
escapes. Escapes of up to three digits are read as before.

Co-authored-by: ybai08 <271969568+ybai08@users.noreply.github.com>
This commit is contained in:
Yang Baiandybai08 authored and GitHub committed 2026-11-04 00:55:48 +01:00
1 parent 5298e8813b
commit 91e269fe5c
2 files changed
+15 -1

No files matched your search

+12
View File
@@ -84,6 +84,18 @@ var gEvalTests = []struct {
[]expr{&callExpr{"echo", []string{"hello\101world"}, 1}},
},
{
`echo "hello\1011world"`,
[]string{"echo", "hello\1011world", "\n"},
[]expr{&callExpr{"echo", []string{"hello\1011world"}, 1}},
},
{
`tty-write "\0337\0338"`,
[]string{"tty-write", "\0337\0338", "\n"},
[]expr{&callExpr{"tty-write", []string{"\0337\0338"}, 1}},
},
{
`echo hello\ world`,
[]string{"echo", "hello world", "\n"},
+3 -1
View File
@@ -237,8 +237,10 @@ scan:
case s.chr == 'v':
buf = append(buf, '\v')
case isDigit(s.chr):
// like C, an octal escape is at most three digits long
// so that "\0337" is read as "\033" followed by "7"
var oct []byte
for isDigit(s.chr) {
for len(oct) < 3 && isDigit(s.chr) {
oct = append(oct, s.chr)
s.next()
}