[Tarantool-patches] [PATCH luajit] Fix debug.debug() for non-string errors.

Sergey Kaplun skaplun at tarantool.org
Thu Dec 30 14:45:13 MSK 2021


From: Mike Pall <mike>

(cherry picked from f5b0fff5a990004375ad43aa6e6c4a11a8b6eb7e)

`lua_tostring()` returns NULL for non-string and non-number objects.
Returned value is passed to `fputs()` without check, so that leads to
crash in case of NULL.

This patch adds the corresponding check. "(error object is not a
string)" is returned in the aforementioned case.

Sergey Kaplun:
* added the description and the test for the problem

Part of tarantool/tarantool#6548
---

Related issue: https://github.com/tarantool/tarantool/issues/6548
Branch: https://github.com/tarantool/luajit/tree/skaplun/gh-noticket-debug-debug-non-string-err
Tarantool branch: https://github.com/tarantool/tarantool/tree/skaplun/gh-noticket-debug-debug-non-string-err-full-ci

 src/lib_debug.c                               |  3 ++-
 .../debug-non-string-error.test.lua           | 26 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 test/tarantool-tests/debug-non-string-error.test.lua

diff --git a/src/lib_debug.c b/src/lib_debug.c
index 8fdfda03..c8f61a43 100644
--- a/src/lib_debug.c
+++ b/src/lib_debug.c
@@ -369,7 +369,8 @@ LJLIB_CF(debug_debug)
       return 0;
     if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
 	lua_pcall(L, 0, 0, 0)) {
-      fputs(lua_tostring(L, -1), stderr);
+      const char *s = lua_tostring(L, -1);
+      fputs(s ? s : "(error object is not a string)", stderr);
       fputs("\n", stderr);
     }
     lua_settop(L, 0);  /* remove eventual returns */
diff --git a/test/tarantool-tests/debug-non-string-error.test.lua b/test/tarantool-tests/debug-non-string-error.test.lua
new file mode 100644
index 00000000..9151dd1a
--- /dev/null
+++ b/test/tarantool-tests/debug-non-string-error.test.lua
@@ -0,0 +1,26 @@
+local tap = require('tap')
+
+local test = tap.test('debug-non-string-error')
+test:plan(1)
+
+local i = 0
+while arg[i] do i = i - 1 end
+local luabin = arg[i + 1]
+
+local magic = 42
+-- XXX: Need \n before print to be interpreted as independend
+-- command.
+local cmd = ([[
+  echo 'error({});
+  print(%d)' | %s -e 'debug.debug()' 2>&1
+]]):format(magic, luabin)
+
+local proc = io.popen(cmd)
+local res = proc:read('*all'):gsub('%s+$', '')
+local ldb = 'lua_debug> '
+local errmsg = '(error object is not a string)'
+-- XXX: lines aren't broken by '\n', so need 2 `ldb`.
+local expected = ldb .. errmsg .. '\n' .. ldb .. ldb .. magic
+test:ok(res == expected, 'handle non-string error in debug.debug()')
+
+os.exit(test:check() and 0 or 1)
-- 
2.34.1



More information about the Tarantool-patches mailing list