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

Add check to avoid runtime error and add tests for go/mysql/fastparse #15000

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go/mysql/fastparse/fastparse.go
dbussink marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func ParseInt64(s string, base int) (int64, error) {
i++
}

if i >= uint(len(s)) {
dbussink marked this conversation as resolved.
Show resolved Hide resolved
return 0, fmt.Errorf("cannot parse int64 from %q", s)
}
minus := s[i] == '-'
if minus {
i++
Expand Down
72 changes: 72 additions & 0 deletions go/mysql/fastparse/fastparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,48 @@ func TestParseInt64(t *testing.T) {
expected: 42,
err: `unparsed tail left after parsing int64 from "\t 42 \n": "\n"`,
},
{
input: "",
base: 10,
expected: 0,
err: `cannot parse int64 from empty string`,
},
{
input: "256",
base: 1,
expected: 0,
err: `invalid base 1; must be in [2, 36]`,
},
{
input: "256",
base: 37,
expected: 0,
err: `invalid base 37; must be in [2, 36]`,
},
{
input: " -",
base: 10,
expected: 0,
err: `cannot parse int64 from " -"`,
},
{
input: "-18446744073709551615",
base: 10,
expected: -9223372036854775808,
err: `cannot parse int64 from "-18446744073709551615": overflow`,
},
{
input: " ",
base: 10,
expected: 0,
err: `cannot parse int64 from " "`,
},
{
input: " :",
base: 10,
expected: 0,
err: `cannot parse int64 from " :"`,
},
}
for _, tc := range testcases {
t.Run(tc.input, func(t *testing.T) {
Expand Down Expand Up @@ -326,6 +368,36 @@ func TestParseUint64(t *testing.T) {
expected: 42,
err: `unparsed tail left after parsing uint64 from "\t 42 \n": "\n"`,
},
{
input: "",
base: 10,
expected: 0,
err: `cannot parse uint64 from empty string`,
},
{
input: "256",
base: 1,
expected: 0,
err: `invalid base 1; must be in [2, 36]`,
},
{
input: "256",
base: 37,
expected: 0,
err: `invalid base 37; must be in [2, 36]`,
},
{
input: " ",
base: 10,
expected: 0,
err: `cannot parse uint64 from " "`,
},
{
input: " :",
base: 10,
expected: 0,
err: `cannot parse uint64 from " :"`,
},
}
for _, tc := range testcases {
t.Run(tc.input, func(t *testing.T) {
Expand Down
Loading