Skip to content

Commit

Permalink
do not rely on undefined behaviour in bytes_test.go
Browse files Browse the repository at this point in the history
8 exbi equals 2^64, therefore it cannot be stored in int64. The tests use
the fact that on x86_64 the following expressions holds true:
int64(0) - 1 == math.MaxInt64.

However, this is not true for other platforms, specifically aarch64, s390x
and ppc64le.

This commit fixes it by testing the library with 7 exbi.

Fixes labstack#37
  • Loading branch information
ondrejbudai committed Aug 26, 2020
1 parent 4919956 commit ed3a1e5
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions bytes/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ func TestBytesParse(t *testing.T) {
assert.Equal(t, int64(12288), b)
}


// KB with space
b, err = Parse("12.25 KB")
if assert.NoError(t, err) {
Expand Down Expand Up @@ -206,22 +205,22 @@ func TestBytesParse(t *testing.T) {
}

// EB
b, err = Parse("8EB")
b, err = Parse("7EB")
if assert.NoError(t, err) {
assert.True(t, math.MaxInt64 == b-1)
assert.Equal(t, 7*int64(1152921504606846976), b)
}
b, err = Parse("8E")
b, err = Parse("7E")
if assert.NoError(t, err) {
assert.True(t, math.MaxInt64 == b-1)
assert.Equal(t, 7*int64(1152921504606846976), b)
}

// EB with spaces
b, err = Parse("8 EB")
b, err = Parse("7 EB")
if assert.NoError(t, err) {
assert.True(t, math.MaxInt64 == b-1)
assert.Equal(t, 7*int64(1152921504606846976), b)
}
b, err = Parse("8 E")
b, err = Parse("7 E")
if assert.NoError(t, err) {
assert.True(t, math.MaxInt64 == b-1)
assert.Equal(t, 7*int64(1152921504606846976), b)
}
}

0 comments on commit ed3a1e5

Please sign in to comment.