Skip to content

Commit

Permalink
DiceDB#921: Added Hashmap incrementFloatValue overflow check (DiceDB#927
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dograprabhav authored Oct 5, 2024
1 parent 3236511 commit e4258b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/eval/hmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (h HashMap) incrementFloatValue(field string, incr float64) (string, error)
return "-1", diceerrors.NewErr(diceerrors.IntOrFloatErr)
}

if (i > 0 && incr > 0 && i > math.MaxFloat64-incr) || (i < 0 && incr < 0 && i < -math.MaxFloat64-incr) {
if math.IsInf(i+incr, 1) || math.IsInf(i+incr, -1) {
return "-1", diceerrors.NewErr(diceerrors.IncrDecrOverflowErr)
}

Expand Down
10 changes: 10 additions & 0 deletions internal/eval/hmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,14 @@ func TestHashMapIncrementFloatValue(t *testing.T) {
val, err = hmap.incrementFloatValue("field2", 1.0)
assert.NotNil(t, err, "Expected error when incrementing a non-float value")
assert.Equal(t, errors.IntOrFloatErr, err.Error(), "Expected int or float error")

inf := math.MaxFloat64

val, err = hmap.incrementFloatValue("field1", inf+float64(1e308))
assert.NotNil(t, err, "Expected error when incrementing a overflowing value")
assert.Equal(t, errors.IncrDecrOverflowErr, err.Error(), "Expected overflow to be detected")

val, err = hmap.incrementFloatValue("field1", -inf-float64(1e308))
assert.NotNil(t, err, "Expected error when incrementing a overflowing value")
assert.Equal(t, errors.IncrDecrOverflowErr, err.Error(), "Expected overflow to be detected")
}

0 comments on commit e4258b3

Please sign in to comment.