From 506f672742328870c1cf93b033593c6791abbcbe Mon Sep 17 00:00:00 2001 From: Binbin Date: Sun, 9 Jul 2023 16:49:36 +0800 Subject: [PATCH] Fix EVAL crashing server when numkeys is -1 (#1568) The check allow we passing -1 is wrong, without this fix, passing a -1 will crash the server. --- src/commands/cmd_script.cc | 2 +- tests/gocase/unit/scripting/scripting_test.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/commands/cmd_script.cc b/src/commands/cmd_script.cc index 4dfbca2b506..54f115dbd74 100644 --- a/src/commands/cmd_script.cc +++ b/src/commands/cmd_script.cc @@ -38,7 +38,7 @@ class CommandEvalImpl : public Commander { int64_t numkeys = GET_OR_RET(ParseInt(args_[2], 10)); if (numkeys > int64_t(args_.size() - 3)) { return {Status::NotOK, "Number of keys can't be greater than number of args"}; - } else if (numkeys < -1) { + } else if (numkeys < 0) { return {Status::NotOK, "Number of keys can't be negative"}; } diff --git a/tests/gocase/unit/scripting/scripting_test.go b/tests/gocase/unit/scripting/scripting_test.go index 930a62103bf..6f8180bb774 100644 --- a/tests/gocase/unit/scripting/scripting_test.go +++ b/tests/gocase/unit/scripting/scripting_test.go @@ -37,6 +37,10 @@ func TestScripting(t *testing.T) { rdb := srv.NewClient() defer func() { require.NoError(t, rdb.Close()) }() + t.Run("EVAL - numkeys can't be negative", func(t *testing.T) { + util.ErrorRegexp(t, rdb.Do(ctx, "EVAL", `return redis.call('PING');`, "-1").Err(), ".*can't be negative.*") + }) + t.Run("EVAL - Does Lua interpreter replies to our requests?", func(t *testing.T) { r := rdb.Eval(ctx, `return 'hello'`, []string{}) require.NoError(t, r.Err())