Skip to content

Commit 76efe12

Browse files
committed
Scripting: Fix regression from redis#1118
The new check-for-number behavior of Lua arguments broke users who use large strings of just integers. The Lua number check would convert the string to a number, but that breaks user data because Lua numbers have limited precision compared to an arbitrarily precise number wrapped in a string. Regression fixed and new test added. Fixes redis#1118 again.
1 parent 8ef79e7 commit 76efe12

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/scripting.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ void luaSortArray(lua_State *lua) {
200200
lua_pop(lua,1); /* Stack: array (sorted) */
201201
}
202202

203+
int luaReallyIsString(lua_State *L, int index) {
204+
int t = lua_type(L, index);
205+
return t == LUA_TSTRING;
206+
}
207+
203208
#define LUA_CMD_OBJCACHE_SIZE 32
204209
#define LUA_CMD_OBJCACHE_MAX_LEN 64
205210
int luaRedisGenericCommand(lua_State *lua, int raise_error) {
@@ -234,7 +239,7 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
234239
size_t obj_len;
235240
char dbuf[64];
236241

237-
if (lua_isnumber(lua,j+1)) {
242+
if (!luaReallyIsString(lua, j+1) && lua_isnumber(lua, j+1)) {
238243
/* We can't use lua_tolstring() for number -> string conversion
239244
* since Lua uses a format specifier that loses precision. */
240245
lua_Number num = lua_tonumber(lua,j+1);

tests/unit/scripting.tcl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ start_server {tags {"scripting"}} {
347347
return redis.call("get","foo")
348348
} 0
349349
} {9007199254740991}
350+
351+
test {String containing number precision test (regression of issue #1118)} {
352+
r eval {
353+
redis.call("set", "key", "12039611435714932082")
354+
return redis.call("get", "key")
355+
} 0
356+
} {12039611435714932082}
350357
}
351358

352359
# Start a new server since the last test in this stanza will kill the

0 commit comments

Comments
 (0)