From 9748751eff1d7bb5401188437f5e694351438bb8 Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Tue, 1 Aug 2023 08:00:31 +0800 Subject: [PATCH] Fix missing pop in ScriptExists --- 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 286b1d4611d..23719b3f5b5 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 a5850e68d9a..dc8bd262725 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 512fb6684fa..5d2a205b036 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 &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);