From a75aa4bf92013445f600297d500fe1852a8532af Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 8 Jan 2016 09:14:13 +0100 Subject: [PATCH] Lua debugger: fix crash printing nested or deep objects. Example of offending code: > script debug yes OK > eval "local a = {1} a[1] = a\nprint(a)" 0 1) * Stopped at 1, stop reason = step over 2) -> 1 local a = {1} a[1] = a > next 1) * Stopped at 2, stop reason = step over 2) -> 2 print(a) > print ... server crash ... Close #2955. --- src/scripting.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/scripting.c b/src/scripting.c index c4d65a7c..6f3f52ba 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -1846,9 +1846,13 @@ void ldbList(int around, int context) { * * The element is not automatically removed from the stack, nor it is * converted to a different type. */ -sds ldbCatStackValue(sds s, lua_State *lua, int idx) { +#define LDB_MAX_VALUES_DEPTH (LUA_MINSTACK/2) +sds ldbCatStackValueRec(sds s, lua_State *lua, int idx, int level) { int t = lua_type(lua,idx); + if (level++ == LDB_MAX_VALUES_DEPTH) + return sdscat(s,""); + switch(t) { case LUA_TSTRING: { @@ -1883,13 +1887,13 @@ sds ldbCatStackValue(sds s, lua_State *lua, int idx) { lua_tonumber(lua,-2) != expected_index)) is_array = 0; /* Stack now: table, key, value */ /* Array repr. */ - repr1 = ldbCatStackValue(repr1,lua,-1); + repr1 = ldbCatStackValueRec(repr1,lua,-1,level); repr1 = sdscatlen(repr1,"; ",2); /* Full repr. */ repr2 = sdscatlen(repr2,"[",1); - repr2 = ldbCatStackValue(repr2,lua,-2); + repr2 = ldbCatStackValueRec(repr2,lua,-2,level); repr2 = sdscatlen(repr2,"]=",2); - repr2 = ldbCatStackValue(repr2,lua,-1); + repr2 = ldbCatStackValueRec(repr2,lua,-1,level); repr2 = sdscatlen(repr2,"; ",2); lua_pop(lua,1); /* Stack: table, key. Ready for next iteration. */ expected_index++; @@ -1926,6 +1930,12 @@ sds ldbCatStackValue(sds s, lua_State *lua, int idx) { return s; } +/* Higher level wrapper for ldbCatStackValueRec() that just uses an initial + * recursion level of '0'. */ +sds ldbCatStackValue(sds s, lua_State *lua, int idx) { + return ldbCatStackValueRec(s,lua,idx,0); +} + /* Produce a debugger log entry representing the value of the Lua object * currently on the top of the stack. The element is ot popped nor modified. * Check ldbCatStackValue() for the actual implementation. */