Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow #361

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions cmd_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,15 @@ func TestIncr(t *testing.T) {
assert(t, err != nil, "do s.Incr error")
}

// Overflow
{
s.Set("overflow", "9223372036854775807")
mustDo(t, c,
"INCR", "overflow",
proto.Error(msgIntOverflow),
)
}

// Wrong usage
{
mustDo(t, c,
Expand Down Expand Up @@ -543,6 +552,20 @@ func TestIncrBy(t *testing.T) {
proto.Error(msgInvalidInt),
)

// Overflow
{
s.Set("overflow", "10")
mustDo(t, c,
"INCRBY", "overflow", "9223372036854775807",
proto.Error(msgIntOverflow),
)
s.Set("overflow", "-10")
mustDo(t, c,
"INCRBY", "overflow", "-9223372036854775807",
proto.Error(msgIntOverflow),
)
}

// Wrong usage
{
mustDo(t, c,
Expand Down Expand Up @@ -684,6 +707,20 @@ func TestDecrBy(t *testing.T) {
proto.Error(msgInvalidInt),
)

// Overflow
{
s.Set("overflow", "10")
mustDo(t, c,
"DECRBY", "overflow", "-9223372036854775807",
proto.Error(msgIntOverflow),
)
s.Set("overflow", "-10")
mustDo(t, c,
"DECRBY", "overflow", "9223372036854775807",
proto.Error(msgIntOverflow),
)
}

// Wrong usage
{
mustDo(t, c,
Expand Down Expand Up @@ -754,6 +791,15 @@ func TestDecr(t *testing.T) {
)
}

// Overflow
{
s.Set("overflow", "-9223372036854775808")
mustDo(t, c,
"DECR", "overflow",
proto.Error(msgIntOverflow),
)
}

// Direct one works
{
s.Set("aap", "400")
Expand Down
18 changes: 18 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"time"
)

const (
intSize = 32 << (^uint(0) >> 63) // 32 or 64

maxInt = 1<<(intSize-1) - 1 // [math.MaxInt] was added in go 1.17
minInt = -1 << (intSize - 1) // [math.MinInt] was added in go 1.17
)

var (
errInvalidEntryID = errors.New("stream ID is invalid")
)
Expand Down Expand Up @@ -180,6 +187,17 @@ func (db *RedisDB) stringIncr(k string, delta int) (int, error) {
return 0, ErrIntValueError
}
}

if delta > 0 {
if maxInt-delta < v {
return 0, ErrIntValueOverflowError
}
} else {
if minInt-delta > v {
return 0, ErrIntValueOverflowError
}
}

v += delta
db.stringSet(k, strconv.Itoa(v))
return v, nil
Expand Down
3 changes: 3 additions & 0 deletions direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var (
// ErrIntValueError can returned by INCRBY
ErrIntValueError = errors.New(msgInvalidInt)

// ErrIntValueOverflowError can be returned by INCR, DECR, INCRBY, DECRBY
ErrIntValueOverflowError = errors.New(msgIntOverflow)

// ErrFloatValueError can returned by INCRBYFLOAT
ErrFloatValueError = errors.New(msgInvalidFloat)
)
Expand Down
11 changes: 11 additions & 0 deletions integration/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,17 @@ func TestIncrAndFriends(t *testing.T) {
c.Do("INCRBYFLOAT", "zero", "12.3")
c.Do("INCRBYFLOAT", "zero", "-13.1")

// Overflow
c.Do("SET", "overflow-up", "9223372036854775807")
c.Error("increment or decrement would overflow", "INCR", "overflow-up")
c.Error("increment or decrement would overflow", "INCRBY", "overflow-up", "1")
c.Error("increment or decrement would overflow", "DECRBY", "overflow-up", "-1")

c.Do("SET", "overflow-down", "-9223372036854775808")
c.Error("increment or decrement would overflow", "DECR", "overflow-down")
c.Error("increment or decrement would overflow", "INCRBY", "overflow-down", "-1")
c.Error("increment or decrement would overflow", "DECRBY", "overflow-down", "1")

// E
c.Do("INCRBYFLOAT", "one", "12e12")
// c.Do("INCRBYFLOAT", "one", "12e34") // FIXME
Expand Down
1 change: 1 addition & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
msgWrongType = "WRONGTYPE Operation against a key holding the wrong kind of value"
msgNotValidHllValue = "WRONGTYPE Key is not a valid HyperLogLog string value."
msgInvalidInt = "ERR value is not an integer or out of range"
msgIntOverflow = "ERR increment or decrement would overflow"
msgInvalidFloat = "ERR value is not a valid float"
msgInvalidMinMax = "ERR min or max is not a float"
msgInvalidRangeItem = "ERR min or max not valid string range item"
Expand Down