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:
1 parent
5298e8813b
commit
91e269fe5c
2 files changed
+15
-1
No files matched your search
@@ -84,6 +84,18 @@ var gEvalTests = []struct {
|
|||||||
[]expr{&callExpr{"echo", []string{"hello\101world"}, 1}},
|
[]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`,
|
`echo hello\ world`,
|
||||||
[]string{"echo", "hello world", "\n"},
|
[]string{"echo", "hello world", "\n"},
|
||||||
|
|||||||
@@ -237,8 +237,10 @@ scan:
|
|||||||
case s.chr == 'v':
|
case s.chr == 'v':
|
||||||
buf = append(buf, '\v')
|
buf = append(buf, '\v')
|
||||||
case isDigit(s.chr):
|
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
|
var oct []byte
|
||||||
for isDigit(s.chr) {
|
for len(oct) < 3 && isDigit(s.chr) {
|
||||||
oct = append(oct, s.chr)
|
oct = append(oct, s.chr)
|
||||||
s.next()
|
s.next()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user