Skip to content

Commit a0c0556

Browse files
Fuslenjoy-binbin
authored andcommitted
Ensure empty error tables in scripts don't crash Valkey (#2229)
When calling the command `EVAL error{} 0`, Valkey crashes with the following stack trace. This patch ensures we never leave the `err_info.msg` field null when we fail to extract a proper error message. --------- Signed-off-by: Fusl <[email protected]> Signed-off-by: Binbin <[email protected]> Co-authored-by: Binbin <[email protected]>
1 parent ad424d6 commit a0c0556

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/script_lua.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,11 @@ void luaExtractErrorInformation(lua_State *lua, errorInfo *err_info) {
16451645
err_info->ignore_err_stats_update = lua_toboolean(lua, -1);
16461646
}
16471647
lua_pop(lua, 1);
1648+
1649+
if (err_info->msg == NULL) {
1650+
/* Ensure we never return a NULL msg. */
1651+
err_info->msg = sdsnew("ERR unknown error");
1652+
}
16481653
}
16491654

16501655
void luaCallFunction(scriptRunCtx* run_ctx, lua_State *lua, robj** keys, size_t nkeys, robj** args, size_t nargs, int debug_enabled) {

tests/unit/scripting.tcl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,4 +2266,21 @@ start_server {tags {"scripting"}} {
22662266
assert { [r memory usage foo] <= $expected_memory};
22672267
}
22682268
}
2269+
2270+
test {EVAL - explicit error() call handling} {
2271+
# error("simple string error")
2272+
assert_error {ERR user_script:1: simple string error script: *} {
2273+
r eval "error('simple string error')" 0
2274+
}
2275+
2276+
# error({"err": "ERR table error"})
2277+
assert_error {ERR table error script: *} {
2278+
r eval "error({err='ERR table error'})" 0
2279+
}
2280+
2281+
# error({})
2282+
assert_error {ERR unknown error script: *} {
2283+
r eval "error({})" 0
2284+
}
2285+
}
22692286
}

0 commit comments

Comments
 (0)