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

fastparse: Fix bug in overflow detection #13702

Merged
merged 1 commit into from
Aug 3, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions go/mysql/fastparse/fastparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ next:
break next
}

var cutoff uint64
switch base {
case 10:
cutoff = math.MaxUint64/10 + 1
case 16:
cutoff = math.MaxUint64/16 + 1
default:
cutoff = math.MaxUint64/uint64(base) + 1
}
if d >= cutoff {
return math.MaxUint64, fmt.Errorf("cannot parse uint64 from %q: %w", s, ErrOverflow)
}
v := d*uint64(base) + uint64(b)
if v < d {
return math.MaxUint64, fmt.Errorf("cannot parse uint64 from %q: %w", s, ErrOverflow)
Expand Down Expand Up @@ -139,6 +151,22 @@ next:
break next
}

var cutoff uint64
switch base {
case 10:
cutoff = math.MaxInt64/10 + 1
case 16:
cutoff = math.MaxInt64/16 + 1
default:
cutoff = math.MaxInt64/uint64(base) + 1
}
if d >= cutoff {
if minus {
return math.MinInt64, fmt.Errorf("cannot parse int64 from %q: %w", s, ErrOverflow)
}
return math.MaxInt64, fmt.Errorf("cannot parse int64 from %q: %w", s, ErrOverflow)
}

dbussink marked this conversation as resolved.
Show resolved Hide resolved
v := d*uint64(base) + uint64(b)
if v < d {
if minus {
Expand Down
12 changes: 12 additions & 0 deletions go/mysql/fastparse/fastparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ func TestParseInt64(t *testing.T) {
expected: 9223372036854775807,
err: `cannot parse int64 from "18446744073709551616": overflow`,
},
{
input: "31415926535897932384",
base: 10,
expected: 9223372036854775807,
err: `cannot parse int64 from "31415926535897932384": overflow`,
},
{
input: "1.1",
base: 10,
Expand Down Expand Up @@ -297,6 +303,12 @@ func TestParseUint64(t *testing.T) {
expected: 18446744073709551615,
err: `cannot parse uint64 from "18446744073709551616": overflow`,
},
{
input: "31415926535897932384",
base: 10,
expected: 18446744073709551615,
err: `cannot parse uint64 from "31415926535897932384": overflow`,
},
dbussink marked this conversation as resolved.
Show resolved Hide resolved
{
input: "1.1",
base: 10,
Expand Down