miniredis
miniredis copied to clipboard
Problem about overflow inspection of INCRBY/DECRBY
Miniredis throws out no error of overflowing when the value becomes greater than math.MaxInt64 after INCRBY or DECRBY (also INCR or DECR). Instead it returns a negative number which was exactly the overflowed value. As a comparison, the real redis instance returns ERR increment or decrement would overflow which thrown out by overflow detection and nothing will be changed.
I found the cause of it and some potential issues as well. I can make a patch to it if it is indeed an unexpected behavior of miniredis.
The following code reproduces the problem.
package main
import(
"log"
"math"
"github.com/alicebob/miniredis/v2"
"github.com/gomodule/resign/redis"
)
func main() {
server, _ := miniredis.Run()
minirds, _ := redis.Dial("tcp", server.Addr())
defer miniredis.Close()
realrds, _ := redis.Dial("tcp", "127.0.0.1:6379") // redis instance
defer realrds.Close()
overflowTest(minirds)
overflowTest(realrds)
}
func overflowTest(r redis.Conn) {
_, _ = r.Do("SET", "number", 100)
rsp, err := redis.Int64(r.Do("INCRBY", "number", math.MaxInt64)
if err != nil {
log.Printf("got an error when exec INCRBY, err=%v", err)
}
log.Printf("got=%d", rsp)
}
Thanks for opening this issue.
I found the cause of it and some potential issues as well. I would like to make a patch to miniredis if it is indeed an unexpected behavior of miniredis.
Sounds like a good improvement to me, a PR would be appreciated.