Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH luajit] Fix debug.debug() for non-string errors.
@ 2021-12-30 11:45 Sergey Kaplun via Tarantool-patches
  2022-06-21 12:01 ` sergos via Tarantool-patches
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2021-12-30 11:45 UTC (permalink / raw)
  To: Sergey Ostanevich, Igor Munkin; +Cc: tarantool-patches

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-06-30 12:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-30 11:45 [Tarantool-patches] [PATCH luajit] Fix debug.debug() for non-string errors Sergey Kaplun via Tarantool-patches
2022-06-21 12:01 ` sergos via Tarantool-patches
2022-06-22  8:24   ` Sergey Kaplun via Tarantool-patches
2022-06-28 23:05 ` Igor Munkin via Tarantool-patches
2022-06-30 12:10 ` Igor Munkin via Tarantool-patches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox