Skip to content

Commit

Permalink
core/chains/evm/assets: fix FuzzWei range detection (#11950)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Feb 7, 2024
1 parent 4cb9669 commit 1df59ca
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions core/chains/evm/assets/wei_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func FuzzWei(f *testing.F) {
f.Add("5 micro")
f.Fuzz(func(t *testing.T, v string) {
if len(v) > 1_000 {
t.Skip()
t.Skipf("too many characters: %d", len(v))
}
if tryParseExp(v) > 4888888 {
t.Skip()
if e := tryParseExp(v); -1000 > e || e > 1000 {
t.Skipf("exponent too large: %d", e)
}
var w Wei
err := w.UnmarshalText([]byte(v))
Expand All @@ -100,9 +100,30 @@ func tryParseExp(v string) int64 {
if i == -1 {
return -1
}
e, err := strconv.ParseInt(v[i+1:], 10, 32)
v = v[i+1:]
if i := strings.IndexFunc(v, func(r rune) bool {
switch {
case r == '-' || r == '+':
return false
case r < '0' || '9' < r:
return true
}
return false
}); i > -1 {
v = v[:i]
}
e, err := strconv.ParseInt(v, 10, 32)
if err != nil {
return -1
}
return e
}

func Test_tryParseExp(t *testing.T) {
got := tryParseExp("000000000E0000000060000000wei")
assert.Equal(t, int64(60000000), got)
got = tryParseExp("0e-80000800")
assert.Equal(t, int64(-80000800), got)
got = tryParseExp("0e+802444440")
assert.Equal(t, int64(802444440), got)
}

0 comments on commit 1df59ca

Please sign in to comment.