Skip to content

Commit

Permalink
avoid panic on unknown escape sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
cenright authored and robfig committed Jul 16, 2020
1 parent ed3d69b commit 54e9f54
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion parse/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func unquoteString(s string) (string, error) {
} else {
replacement, ok := unescapes[r]
if !ok {
return "", errors.New("unrecognized escape code: \\" + s[i:i+1])
return "", errors.New("unrecognized escape code: \\" + s[i-1:i])
}
r = rune(replacement)
}
Expand Down
14 changes: 14 additions & 0 deletions parse/quote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,17 @@ func TestUnquote(t *testing.T) {
}
}
}

func TestUnquoteUnrecognized(t *testing.T) {
var tests = []string{
`'\0'`,
`'\a'`,
`'\z'`,
}
for _, tc := range tests {
_, err := unquoteString(tc)
if err == nil {
t.Errorf("expected unrecognized escape sequence %s to fail", tc)
}
}
}

0 comments on commit 54e9f54

Please sign in to comment.