Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode: ensure signed numbers don't start with an underscore #698

Merged
merged 1 commit into from Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions decode.go
Expand Up @@ -386,8 +386,13 @@ func parseIntDec(b []byte) (int64, error) {
}

func checkAndRemoveUnderscoresIntegers(b []byte) ([]byte, error) {
if b[0] == '_' {
return nil, newDecodeError(b[0:1], "number cannot start with underscore")
start := 0
if b[start] == '+' || b[start] == '-' {
start++
}

if b[start] == '_' {
return nil, newDecodeError(b[start:start+1], "number cannot start with underscore")
}

if b[len(b)-1] == '_' {
Expand Down
10 changes: 9 additions & 1 deletion unmarshaler_test.go
Expand Up @@ -2306,9 +2306,17 @@ func TestUnmarshalDecodeErrors(t *testing.T) {
data: `flt8 = 224_617.445_991__228`,
},
{
desc: "float with double _",
desc: "float with double .",
data: `flt8 = 1..2`,
},
{
desc: "number with plus sign and leading underscore",
data: `a = +_0`,
},
{
desc: "number with negative sign and leading underscore",
data: `a = -_0`,
},
{
desc: "int with wrong base",
data: `a = 0f2`,
Expand Down