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

* Re: [Tarantool-patches] [PATCH luajit] Fix debug.debug() for non-string errors.
  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
  2 siblings, 1 reply; 5+ messages in thread
From: sergos via Tarantool-patches @ 2022-06-21 12:01 UTC (permalink / raw)
  To: Sergey Kaplun; +Cc: tarantool-patches

[-- Attachment #1: Type: text/plain, Size: 3814 bytes --]

Hi!

Thanks for the patch!

It’s strange, but Tarantool happens to handle this case correctly?

% echo 'error({}); print(42)' | ./src/tarantool -e 'debug.debug()' 
lua_debug> (null)
lua_debug> %                                                                                                                                                                                                                                                              % echo $?
0

Surely, with the patch applied I see the following, as expected:

% echo 'error({}); print(42)' | ./src/tarantool -e 'debug.debug()'
lua_debug> (error object is not a string)
lua_debug> %                                                                                                                                                                                                                                                              % 


Is it relevant at all?

regards,
Sergos


> On 30 Dec 2021, at 14:45, Sergey Kaplun <skaplun@tarantool.org> wrote:
> 
> 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
> 


[-- Attachment #2: Type: text/html, Size: 9318 bytes --]

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

* Re: [Tarantool-patches] [PATCH luajit] Fix debug.debug() for non-string errors.
  2022-06-21 12:01 ` sergos via Tarantool-patches
@ 2022-06-22  8:24   ` Sergey Kaplun via Tarantool-patches
  0 siblings, 0 replies; 5+ messages in thread
From: Sergey Kaplun via Tarantool-patches @ 2022-06-22  8:24 UTC (permalink / raw)
  To: sergos; +Cc: tarantool-patches

Hi, Sergos!

Thanks for the review!

On 21.06.22, sergos wrote:
> Hi!
> 
> Thanks for the patch!
> 
> It’s strange, but Tarantool happens to handle this case correctly?

Not for me:).

| $ echo 'error({}); print(42)' | ./src/tarantool -e 'debug.debug()'
| lua_debug> Segmentation fault
| ...
| #5  0x55a6323a6b84 in lj_cf_debug_debug+ef
| #6  0x55a63236b66f in lj_BC_FUNCC+34
| #7  0x55a63237687b in lua_pcall+379
| #8  0x55a632305317 in luaT_call+29
| #9  0x55a6322fd8b7 in run_script_f+427
| #10 0x55a6320f496e in _ZL16fiber_cxx_invokePFiP13__va_list_tagES0_+1e
| #11 0x55a63232b3f0 in fiber_loop+8d
| #12 0x55a632686716 in coro_init+4c

May be you should build it with debug?

| $ src/tarantool -v
| Tarantool 2.10.0-beta2-6-g634f59c7f
| Target: Linux-x86_64-Debug
| Build options: cmake . -DCMAKE_INSTALL_PREFIX=/usr/local -DENABLE_BACKTRACE=ON

P.S. I use not the latest Tarantool as far as there is no related
patches (AFAIK).

> 
> % echo 'error({}); print(42)' | ./src/tarantool -e 'debug.debug()' 
> lua_debug> (null)
> lua_debug> %                                                                                                                                                                                                                                                              % echo $?
> 0
> 
> Surely, with the patch applied I see the following, as expected:
> 
> % echo 'error({}); print(42)' | ./src/tarantool -e 'debug.debug()'
> lua_debug> (error object is not a string)
> lua_debug> %                                                                                                                                                                                                                                                              % 
> 
> 
> Is it relevant at all?
> 
> regards,
> Sergos
> 
> 
> > On 30 Dec 2021, at 14:45, Sergey Kaplun <skaplun@tarantool.org> wrote:
> > 

<snipped>

> 

-- 
Best regards,
Sergey Kaplun

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

* Re: [Tarantool-patches] [PATCH luajit] Fix debug.debug() for non-string errors.
  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-28 23:05 ` Igor Munkin via Tarantool-patches
  2022-06-30 12:10 ` Igor Munkin via Tarantool-patches
  2 siblings, 0 replies; 5+ messages in thread
From: Igor Munkin via Tarantool-patches @ 2022-06-28 23:05 UTC (permalink / raw)
  To: Sergey Kaplun; +Cc: tarantool-patches

Sergey,

Thanks for your patch! LGTM, but I've tweaked the test a bit[1].

[1]: https://github.com/tarantool/luajit/commit/adc6089

-- 
Best regards,
IM

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

* Re: [Tarantool-patches] [PATCH luajit] Fix debug.debug() for non-string errors.
  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-28 23:05 ` Igor Munkin via Tarantool-patches
@ 2022-06-30 12:10 ` Igor Munkin via Tarantool-patches
  2 siblings, 0 replies; 5+ messages in thread
From: Igor Munkin via Tarantool-patches @ 2022-06-30 12:10 UTC (permalink / raw)
  To: Sergey Kaplun; +Cc: tarantool-patches

Sergey,

I've checked the patch into all long-term branches in tarantool/luajit
and bumped a new version in master, 2.10 and 1.10.

On 30.12.21, Sergey Kaplun wrote:
> 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
> 

<snipped>

> -- 
> 2.34.1
> 

-- 
Best regards,
IM

^ 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