Skip to content
Open
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
10 changes: 6 additions & 4 deletions internal/bytesconv/bytesconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
package bytesconv

import (
"math"
"net/http"
"time"
"unsafe"
Expand Down Expand Up @@ -162,12 +163,13 @@ func ParseUintBuf(b []byte) (int, int, error) {
}
return v, i, nil
}
vNew := 10*v + int(k)
// Test for overflow.
if vNew < v {
// Test for overflow before multiplying, because 10*v may wrap around
// to a value that is still greater than v, which would make a
// post-multiplication check unreliable.
if v > (math.MaxInt-int(k))/10 {
return -1, i, errTooLongInt
}
v = vNew
v = 10*v + int(k)
}
return v, n, nil
}
Expand Down
6 changes: 6 additions & 0 deletions internal/bytesconv/bytesconv_64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ func TestParseUintError(t *testing.T) {
{"-9223372036854775808"},
{"9223372036854775808"},
{"18446744073709551615"},
// Values whose intermediate 10*v wraps past 2^64 and lands back on a
// positive number, which a post-multiplication overflow check misses.
{"21000000000000000000"},
{"25000000000000000000"},
{"46000000000000000000"},
{"83000000000000000000"},
} {
n, err := ParseUint(S2b(v.s))
if err == nil {
Expand Down