Skip to content

Commit

Permalink
[#91] Fixed the behavior of checkedAdd when over/underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
maypok86 committed Jul 28, 2024
1 parent e57f61b commit a0e9d7f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ func checkedAdd(a, b int64) int64 {
return naiveSum
}
// we did over/under flow, if the sign is negative we should return math.MaxInt64 otherwise math.MinInt64.
return math.MaxInt64 + ((naiveSum >> 63) ^ 1)
if naiveSum < 0 {
return math.MaxInt64
}
return math.MinInt64
}

func negativeToMax(v int64) int64 {
Expand Down
27 changes: 27 additions & 0 deletions stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package otter
import (
"math"
"testing"
"unsafe"
)

func TestStats(t *testing.T) {
Expand Down Expand Up @@ -59,3 +60,29 @@ func TestStats(t *testing.T) {
t.Fatalf("not valid evicted cost. want %d, got %d", expected, s.EvictedCost())
}
}

func TestCheckedAdd_Overflow(t *testing.T) {
if unsafe.Sizeof(t) != 8 {
t.Skip()
}

a := int64(math.MaxInt64)
b := int64(23)

if got := checkedAdd(a, b); got != math.MaxInt64 {
t.Fatalf("wrong overflow in checkedAdd. want %d, got %d", math.MaxInt64, got)

Check failure on line 73 in stats_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.x)

cannot use math.MaxInt64 (untyped int constant 9223372036854775807) as int value in argument to t.Fatalf (overflows)

Check failure on line 73 in stats_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

cannot use math.MaxInt64 (untyped int constant 9223372036854775807) as int value in argument to t.Fatalf (overflows)

Check failure on line 73 in stats_test.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

cannot use math.MaxInt64 (untyped int constant 9223372036854775807) as int value in argument to t.Fatalf (overflows)

Check failure on line 73 in stats_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

cannot use math.MaxInt64 (untyped int constant 9223372036854775807) as int value in argument to t.Fatalf (overflows)
}
}

func TestCheckedAdd_Underflow(t *testing.T) {
if unsafe.Sizeof(t) != 8 {
t.Skip()
}

a := int64(math.MinInt64 + 10)
b := int64(-23)

if got := checkedAdd(a, b); got != math.MinInt64 {
t.Fatalf("wrong underflow in checkedAdd. want %d, got %d", math.MinInt64, got)

Check failure on line 86 in stats_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.x)

cannot use math.MinInt64 (untyped int constant -9223372036854775808) as int value in argument to t.Fatalf (overflows)

Check failure on line 86 in stats_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x)

cannot use math.MinInt64 (untyped int constant -9223372036854775808) as int value in argument to t.Fatalf (overflows)

Check failure on line 86 in stats_test.go

View workflow job for this annotation

GitHub Actions / test (1.21.x)

cannot use math.MinInt64 (untyped int constant -9223372036854775808) as int value in argument to t.Fatalf (overflows)

Check failure on line 86 in stats_test.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

cannot use math.MinInt64 (untyped int constant -9223372036854775808) as int value in argument to t.Fatalf (overflows)
}
}

0 comments on commit a0e9d7f

Please sign in to comment.