Merge pull request #1024 from filecoin-project/fix/sizestr-overflow

Fix SizeStr index out of band
This commit is contained in:
Łukasz Magiera 2020-01-07 14:30:15 +01:00 committed by GitHub
commit 2635028933
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -131,7 +131,7 @@ func (bi BigInt) SizeStr() string {
den := big.NewRat(1, 1024)
var i int
for f, _ := r.Float64(); f >= 1024 && 1 < len(sizeUnits); f, _ = r.Float64() {
for f, _ := r.Float64(); f >= 1024 && i+1 < len(sizeUnits); f, _ = r.Float64() {
i++
r = r.Mul(r, den)
}

View File

@ -2,6 +2,7 @@ package types
import (
"bytes"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
@ -69,3 +70,11 @@ func TestSizeStr(t *testing.T) {
assert.Equal(t, c.out, NewInt(c.in).SizeStr(), "input %+v, produced wrong result", c)
}
}
func TestSizeStrBig(t *testing.T) {
ZiB := big.NewInt(50000)
ZiB = ZiB.Lsh(ZiB, 70)
assert.Equal(t, "5e+04 ZiB", BigInt{Int: ZiB}.SizeStr(), "inout %+v, produced wrong result", ZiB)
}