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 2c5a57a5 Fix missing pop in ScriptExists (#1626)
2c5a57a5 is described below

commit 2c5a57a57b2aacc1b8a46fa6ffd98e059596bb8a
Author: Twice <[email protected]>
AuthorDate: Tue Aug 1 19:48:01 2023 +0800

    Fix missing pop in ScriptExists (#1626)
    
    Co-authored-by: hulk <[email protected]>
---
 src/server/server.cc     | 5 +----
 src/storage/scripting.cc | 7 +++++++
 src/storage/scripting.h  | 2 ++
 3 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/server/server.cc b/src/server/server.cc
index 286b1d46..23719b3f 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -40,7 +40,6 @@
 #include "commands/commander.h"
 #include "config.h"
 #include "fmt/format.h"
-#include "lua.h"
 #include "redis_connection.h"
 #include "storage/compaction_checker.h"
 #include "storage/redis_db.h"
@@ -1507,11 +1506,9 @@ Status Server::LookupAndCreateCommand(const std::string 
&cmd_name, std::unique_p
 }
 
 Status Server::ScriptExists(const std::string &sha) {
-  lua_getglobal(lua_, (REDIS_LUA_FUNC_SHA_PREFIX + sha).c_str());
-  if (!lua_isnil(lua_, -1)) {
+  if (lua::ScriptExists(lua_, sha)) {
     return Status::OK();
   }
-  lua_pop(lua_, 1);
 
   std::string body;
   return ScriptGet(sha, &body);
diff --git a/src/storage/scripting.cc b/src/storage/scripting.cc
index a5850e68..dc8bd262 100644
--- a/src/storage/scripting.cc
+++ b/src/storage/scripting.cc
@@ -33,6 +33,7 @@
 #include "lua.h"
 #include "parse_util.h"
 #include "rand.h"
+#include "scope_exit.h"
 #include "server/redis_connection.h"
 #include "server/server.h"
 #include "sha1.h"
@@ -292,6 +293,12 @@ Status EvalGenericCommand(redis::Connection *conn, const 
std::string &body_or_sh
   return Status::OK();
 }
 
+bool ScriptExists(lua_State *lua, const std::string &sha) {
+  lua_getglobal(lua, (REDIS_LUA_FUNC_SHA_PREFIX + sha).c_str());
+  auto exit = MakeScopeExit([lua] { lua_pop(lua, 1); });
+  return !lua_isnil(lua, -1);
+}
+
 int RedisCallCommand(lua_State *lua) { return RedisGenericCommand(lua, 1); }
 
 int RedisPCallCommand(lua_State *lua) { return RedisGenericCommand(lua, 0); }
diff --git a/src/storage/scripting.h b/src/storage/scripting.h
index 512fb668..5d2a205b 100644
--- a/src/storage/scripting.h
+++ b/src/storage/scripting.h
@@ -53,6 +53,8 @@ Status EvalGenericCommand(redis::Connection *conn, const 
std::string &body_or_sh
                           const std::vector<std::string> &argv, bool evalsha, 
std::string *output,
                           bool read_only = false);
 
+bool ScriptExists(lua_State *lua, const std::string &sha);
+
 const char *RedisProtocolToLuaType(lua_State *lua, const char *reply);
 const char *RedisProtocolToLuaTypeInt(lua_State *lua, const char *reply);
 const char *RedisProtocolToLuaTypeBulk(lua_State *lua, const char *reply);

Reply via email to