<HTML><BODY><div>Hi, Sergey!</div><div>Thanks for the patch!</div><div>LGTM</div><div> </div><div> </div><div data-signature-widget="container"><div data-signature-widget="content"><div>--<br>Best regards,</div><div>Maxim Kokryashkin</div></div></div><div> </div><div> </div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;">Пятница, 6 октября 2023, 17:16 +03:00 от Sergey Bronnikov via Tarantool-patches <tarantool-patches@dev.tarantool.org>:<br> <div id=""><div class="js-helper js-readmsg-msg"><div><div id="style_16966017921179153011_BODY">From: Sergey Bronnikov <<a href="/compose?To=sergeyb@tarantool.org">sergeyb@tarantool.org</a>><br><br>Reported by Mathias Westerdahl.<br><br>(cherry picked from commit 633f265f67f322cbe2c5fd11d3e46d968ac220f7)<br><br>Lua 5.1 Reference Manual [1] defines a function `lua_concat`, that:<br> <div class="mail-quote-collapse">> void lua_concat (lua_State *L, int n);<br>><br>> Concatenates the n values at the top of the stack, pops them, and leaves<br>> the result at the top.</div><br>Without the patch `lua_concat()` behaved incorrectly with userdata with<br>defined `__concat` metamethod. The problem is GC64-specific.<br><br>Assuming we have three literals and a userdata with defined "__concat"<br>metamethod on top of the Lua stack:<br><br>1 [string]<br>2 [string]<br>3 [string]<br>4 [string]<br>5 [userdata] <--- top<br><br>On attempt to concatenate *two* items on top of the Lua stack,<br>`lua_concat()` concatenates *four* items and leaves result on the top:<br><br>1 [string]<br>2 [string][string][string][userdata] <--- top<br><br>The problem is in incorrect calculation of `n` counter in the loop in<br>implementation of function `lua_concat`. Without the fix `n` is equal to<br>3 at the end of the first iteration and therefore it goes to the next<br>iteration of concatenation. In the fixed implementation of<br>`lua_concat()` `n` is equal to 1 at the end of the first loop iteration,<br>decremented in a loop postcondition and breaks the loop.<br><br>The patch fixes incorrect behaviour.<br><br>1. <a href="https://www.lua.org/manual/5.1/manual.html" target="_blank">https://www.lua.org/manual/5.1/manual.html</a><br><br>Sergey Bronnikov:<br>* added the description and the test for the problem<br><br>Part of tarantool/tarantool#8825<br>---<br>PR: <a href="https://github.com/tarantool/tarantool/pull/9176" target="_blank">https://github.com/tarantool/tarantool/pull/9176</a><br>Branch: <a href="https://github.com/tarantool/luajit/commits/ligurio/lj-881-fix-concat" target="_blank">https://github.com/tarantool/luajit/commits/ligurio/lj-881-fix-concat</a><br>Issues:<br>- <a href="https://github.com/LuaJIT/LuaJIT/issues/881" target="_blank">https://github.com/LuaJIT/LuaJIT/issues/881</a><br>- <a href="https://github.com/tarantool/tarantool/issues/8825" target="_blank">https://github.com/tarantool/tarantool/issues/8825</a><br><br>Changes:<br>- fixed typos<br>- updated assertions (bool asserts replaced with int assertions)<br>- replaced userdata with struct with userdata with integer per Max's request<br>that's all, folks<br><br> src/lj_api.c | 2 +-<br> .../lj-881-fix-lua-concat.test.c | 95 +++++++++++++++++++<br> 2 files changed, 96 insertions(+), 1 deletion(-)<br> create mode 100644 test/tarantool-c-tests/lj-881-fix-lua-concat.test.c<br><br>diff --git a/src/lj_api.c b/src/lj_api.c<br>index 05e02029..3bacad33 100644<br>--- a/src/lj_api.c<br>+++ b/src/lj_api.c<br>@@ -801,7 +801,7 @@ LUA_API void lua_concat(lua_State *L, int n)<br> L->top -= n;<br> break;<br> }<br>- n -= (int)(L->top - top);<br>+ n -= (int)(L->top - (top - 2*LJ_FR2));<br> L->top = top+2;<br> jit_secure_call(L, top, 1+1);<br> L->top -= 1+LJ_FR2;<br>diff --git a/test/tarantool-c-tests/lj-881-fix-lua-concat.test.c b/test/tarantool-c-tests/lj-881-fix-lua-concat.test.c<br>new file mode 100644<br>index 00000000..1b1f146f<br>--- /dev/null<br>+++ b/test/tarantool-c-tests/lj-881-fix-lua-concat.test.c<br>@@ -0,0 +1,95 @@<br>+#include <string.h><br>+<br>+#include "lua.h"<br>+#include "lauxlib.h"<br>+<br>+#include "test.h"<br>+#include "utils.h"<br>+<br>+/*<br>+ * This test demonstrates LuaJIT's incorrect behaviour, when<br>+ * calling `lua_concat()` with userdata with __concat metamethod.<br>+ * See <a href="https://github.com/LuaJIT/LuaJIT/issues/881" target="_blank">https://github.com/LuaJIT/LuaJIT/issues/881</a> for details.<br>+ */<br>+<br>+#define MT_NAME "int"<br>+<br>+static int INFO_concat(lua_State *L)<br>+{<br>+ const char *s = luaL_checkstring(L, 1);<br>+ int *n = (int *)luaL_checkudata(L, 2, MT_NAME);<br>+ /* Do non-default concatenation. */<br>+ lua_pushfstring(L, "%s + %d", s, *n);<br>+ return 1;<br>+}<br>+<br>+static const luaL_Reg mt[] =<br>+{<br>+ {"__concat", INFO_concat},<br>+ {NULL, NULL}<br>+};<br>+<br>+static int lua_concat_testcase(void *test_state)<br>+{<br>+ /* Setup. */<br>+ lua_State *L = test_state;<br>+ const int top = 4;<br>+<br>+ /* Create metatable and put it to the Lua registry. */<br>+ luaL_newmetatable(L, MT_NAME);<br>+ /* Fill metatable. */<br>+ luaL_register(L, 0, mt);<br>+ lua_pop(L, 1);<br>+<br>+ assert_int_equal(lua_gettop(L), 0);<br>+<br>+ lua_pushliteral(L, "C");<br>+ lua_pushliteral(L, "B");<br>+ lua_pushliteral(L, "A");<br>+<br>+ int *n = (int *)lua_newuserdata(L, sizeof(int));<br>+ *n = 100;<br>+<br>+ luaL_getmetatable(L, MT_NAME);<br>+ lua_setmetatable(L, -2);<br>+<br>+ assert_int_equal(lua_gettop(L), top);<br>+<br>+ /* Test body. */<br>+<br>+ /*<br>+ * void lua_concat (lua_State *L, int n);<br>+ *<br>+ * Concatenates the n values at the top of the stack,<br>+ * pops them, and leaves the result at the top. If n is 1,<br>+ * the result is the single value on the stack; if n is 0,<br>+ * the result is the empty string [1].<br>+ *<br>+ * 1. <a href="https://www.lua.org/manual/5.1/manual.html" target="_blank">https://www.lua.org/manual/5.1/manual.html</a><br>+ */<br>+<br>+ /* Concatenate two elements on top. */<br>+ lua_concat(L, 2);<br>+<br>+ const char *str = lua_tostring(L, -1);<br>+ assert_int_equal(lua_gettop(L), top - 2 + 1);<br>+ const char expected_str[] = "A + 100";<br>+ assert_str_equal(str, expected_str, strlen(expected_str));<br>+<br>+ /* Teardown. */<br>+ lua_settop(L, 0);<br>+<br>+ return TEST_EXIT_SUCCESS;<br>+}<br>+<br>+int main(void)<br>+{<br>+ lua_State *L = utils_lua_init();<br>+ const struct test_unit tgroup[] = {<br>+ test_unit_def(lua_concat_testcase),<br>+ };<br>+ const int test_result = test_run_group(tgroup, L);<br>+ utils_lua_close(L);<br>+<br>+ return test_result;<br>+}<br>--<br>2.34.1</div></div></div></div></blockquote><div> </div></BODY></HTML>