This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 953de0e9 chore(server): check the arity should happen before the flag 
generate (#2736)
953de0e9 is described below

commit 953de0e961bb54239287546a8d251c5d82cb0120
Author: hulk <[email protected]>
AuthorDate: Sat Jan 25 22:24:30 2025 +0800

    chore(server): check the arity should happen before the flag generate 
(#2736)
---
 src/server/redis_connection.cc                | 16 ++++++++--------
 src/storage/scripting.cc                      | 10 +++++-----
 tests/gocase/unit/scripting/function_test.go  |  2 +-
 tests/gocase/unit/scripting/scripting_test.go |  4 ++--
 4 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/src/server/redis_connection.cc b/src/server/redis_connection.cc
index 092df73d..62028651 100644
--- a/src/server/redis_connection.cc
+++ b/src/server/redis_connection.cc
@@ -397,8 +397,15 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> 
*to_process_cmds) {
 
     const auto &attributes = current_cmd->GetAttributes();
     auto cmd_name = attributes->name;
-    auto cmd_flags = attributes->GenerateFlags(cmd_tokens);
 
+    int tokens = static_cast<int>(cmd_tokens.size());
+    if (!attributes->CheckArity(tokens)) {
+      if (is_multi_exec) multi_error_ = true;
+      Reply(redis::Error({Status::NotOK, "wrong number of arguments"}));
+      continue;
+    }
+
+    auto cmd_flags = attributes->GenerateFlags(cmd_tokens);
     if (GetNamespace().empty()) {
       if (!password.empty()) {
         if (!(cmd_flags & kCmdAuth)) {
@@ -431,13 +438,6 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> 
*to_process_cmds) {
       continue;
     }
 
-    int tokens = static_cast<int>(cmd_tokens.size());
-    if (!attributes->CheckArity(tokens)) {
-      if (is_multi_exec) multi_error_ = true;
-      Reply(redis::Error({Status::NotOK, "wrong number of arguments"}));
-      continue;
-    }
-
     current_cmd->SetArgs(cmd_tokens);
     auto s = current_cmd->Parse();
     if (!s.IsOK()) {
diff --git a/src/storage/scripting.cc b/src/storage/scripting.cc
index 19b13854..40002827 100644
--- a/src/storage/scripting.cc
+++ b/src/storage/scripting.cc
@@ -755,6 +755,11 @@ int RedisGenericCommand(lua_State *lua, int raise_error) {
   auto cmd = *std::move(cmd_s);
 
   auto attributes = cmd->GetAttributes();
+  if (!attributes->CheckArity(argc)) {
+    PushError(lua, "Wrong number of args calling Redis command From Lua 
script");
+    return raise_error ? RaiseError(lua) : 1;
+  }
+
   auto cmd_flags = attributes->GenerateFlags(args);
 
   if ((script_run_ctx->flags & ScriptFlagType::kScriptNoWrites) && !(cmd_flags 
& redis::kCmdReadOnly)) {
@@ -762,11 +767,6 @@ int RedisGenericCommand(lua_State *lua, int raise_error) {
     return raise_error ? RaiseError(lua) : 1;
   }
 
-  if (!attributes->CheckArity(argc)) {
-    PushError(lua, "Wrong number of args calling Redis command From Lua 
script");
-    return raise_error ? RaiseError(lua) : 1;
-  }
-
   if ((cmd_flags & redis::kCmdNoScript) || (cmd_flags & redis::kCmdExclusive)) 
{
     PushError(lua, "This Redis command is not allowed from scripts");
     return raise_error ? RaiseError(lua) : 1;
diff --git a/tests/gocase/unit/scripting/function_test.go 
b/tests/gocase/unit/scripting/function_test.go
index 9ab7eff0..43043a61 100644
--- a/tests/gocase/unit/scripting/function_test.go
+++ b/tests/gocase/unit/scripting/function_test.go
@@ -516,7 +516,7 @@ func TestFunctionScriptFlags(t *testing.T) {
                { 'no-writes', 'allow-cross-slot-keys' })
 
                redis.register_function('no_write_allow_cross_func_2', 
-               function() redis.call('set', 'bar'); return redis.call('set', 
'test'); end, 
+               function() redis.call('set', 'bar', 'value'); return 
redis.call('set', 'test'); end,
                { 'no-writes', 'allow-cross-slot-keys' })
 
                redis.register_function('no_write_allow_cross_func_3', 
diff --git a/tests/gocase/unit/scripting/scripting_test.go 
b/tests/gocase/unit/scripting/scripting_test.go
index 20aa0760..cd04351a 100644
--- a/tests/gocase/unit/scripting/scripting_test.go
+++ b/tests/gocase/unit/scripting/scripting_test.go
@@ -876,8 +876,8 @@ func TestEvalScriptFlags(t *testing.T) {
 
                r = rdb0.Do(ctx, "EVAL",
                        `#!lua flags=no-writes,allow-cross-slot-keys
-               redis.call('set', 'bar');
-               return redis.call('set', 'test');`, "0")
+               redis.call('set', 'bar', 'value');
+               return redis.call('set', 'test', 'value');`, "0")
                util.ErrorRegexp(t, r.Err(), "ERR .* Write commands are not 
allowed from read-only scripts")
 
                r = rdb0.Do(ctx, "EVAL",

Reply via email to