Skip to content

Commit adf0a34

Browse files
sundbFuslenjoy-binbin
authored andcommitted
Ensure empty error tables in scripts don't crash (redis#14163)
This PR is based on: valkey-io/valkey#2229 When calling the command `EVAL error{} 0`, Redis 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: Fusl <[email protected]> Co-authored-by: Binbin <[email protected]>
1 parent 2f09224 commit adf0a34

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/script_lua.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
* Copyright (c) 2009-Present, Redis Ltd.
33
* All rights reserved.
44
*
5+
* Copyright (c) 2024-present, Valkey contributors.
6+
* All rights reserved.
7+
*
58
* Licensed under your choice of (a) the Redis Source Available License 2.0
69
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
710
* GNU Affero General Public License v3 (AGPLv3).
11+
*
12+
* Portions of this file are available under BSD3 terms; see REDISCONTRIBUTIONS for more information.
813
*/
914

1015
#include "script_lua.h"
@@ -1580,6 +1585,9 @@ void luaExtractErrorInformation(lua_State *lua, errorInfo *err_info) {
15801585
lua_getfield(lua, -1, "err");
15811586
if (lua_isstring(lua, -1)) {
15821587
err_info->msg = sdsnew(lua_tostring(lua, -1));
1588+
} else {
1589+
/* Ensure we never return a NULL msg. */
1590+
err_info->msg = sdsnew("ERR unknown error");
15831591
}
15841592
lua_pop(lua, 1);
15851593

tests/unit/scripting.tcl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
#
2+
# Copyright (c) 2009-Present, Redis Ltd.
3+
# All rights reserved.
4+
#
5+
# Copyright (c) 2024-present, Valkey contributors.
6+
# All rights reserved.
7+
#
8+
# Licensed under your choice of (a) the Redis Source Available License 2.0
9+
# (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
10+
# GNU Affero General Public License v3 (AGPLv3).
11+
#
12+
# Portions of this file are available under BSD3 terms; see REDISCONTRIBUTIONS for more information.
13+
#
14+
115
foreach is_eval {0 1} {
216

317
if {$is_eval == 1} {
@@ -2447,4 +2461,21 @@ start_server {tags {"scripting"}} {
24472461
assert { [r memory usage foo] <= $expected_memory};
24482462
}
24492463
}
2464+
2465+
test {EVAL - explicit error() call handling} {
2466+
# error("simple string error")
2467+
assert_error {ERR user_script:1: simple string error script: *} {
2468+
r eval "error('simple string error')" 0
2469+
}
2470+
2471+
# error({"err": "ERR table error"})
2472+
assert_error {ERR table error script: *} {
2473+
r eval "error({err='ERR table error'})" 0
2474+
}
2475+
2476+
# error({})
2477+
assert_error {ERR unknown error script: *} {
2478+
r eval "error({})" 0
2479+
}
2480+
}
24502481
}

0 commit comments

Comments
 (0)