Skip to content

Commit

Permalink
[release-branch.go1.16] math/big: prevent overflow in (*Rat).SetString
Browse files Browse the repository at this point in the history
Credit to rsc@ for the original patch.

Thanks to the OSS-Fuzz project for discovering this
issue and to Emmanuel Odeke (@odeke_et) for reporting it.

Updates #50699
Fixes #50700
Fixes CVE-2022-23772

Change-Id: I590395a3d55689625390cf1e58f5f40623b26ee5
Reviewed-on: https://go-review.googlesource.com/c/go/+/379537
Trust: Katie Hockman <katie@golang.org>
Run-TryBot: Katie Hockman <katie@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
(cherry picked from commit ad345c2)
Reviewed-on: https://go-review.googlesource.com/c/go/+/381337
  • Loading branch information
katiehockman committed Jan 28, 2022
1 parent 6cbcf58 commit 07ee9e6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/math/big/ratconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ func (z *Rat) SetString(s string) (*Rat, bool) {
n := exp5
if n < 0 {
n = -n
if n < 0 {
// This can occur if -n overflows. -(-1 << 63) would become
// -1 << 63, which is still negative.
return nil, false
}
}
if n > 1e6 {
return nil, false // avoid excessively large exponents
Expand Down
1 change: 1 addition & 0 deletions src/math/big/ratconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ var setStringTests = []StringTest{
{in: "4/3/"},
{in: "4/3."},
{in: "4/"},
{in: "13e-9223372036854775808"}, // CVE-2022-23772

// valid
{"0", "0", true},
Expand Down

0 comments on commit 07ee9e6

Please sign in to comment.