From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 9D3A956C777; Mon, 9 Oct 2023 17:15:40 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9D3A956C777 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1696860940; bh=/mTndwmQIMkv2dckGq6CEjAM4s5yPuXnNrMGOOxQ9dY=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=PN8VVmT/DgzRCiN25XvKyBVLoCeyrXDalYfgSuAyGOKGahD5ziJOmMkolk9OxuOew +LMnxlLhSpO8GiWTRfETmGMsEgFT3PIjAGveki1Kp1l5P3pWzGMJ9Es+M88b3GSyKM MH1t2v1oUxuATbWSZPmmJU1GuUusnLfCR/SoMgNI= Received: from smtpng3.i.mail.ru (smtpng3.i.mail.ru [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 9BC2756C777 for ; Mon, 9 Oct 2023 17:15:38 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9BC2756C777 Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1qpr2n-000477-N6; Mon, 09 Oct 2023 17:15:38 +0300 Date: Mon, 9 Oct 2023 17:11:09 +0300 To: Sergey Bronnikov Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9FE0487E502468146058F26F7CE93BB20EB0123E92217C24200894C459B0CD1B9D939E84646A415F16F6779064A378112967BCD7238EC63E7ACA8F312C2F0F594 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE77BF46084C0059042EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637217E865CDB7D48CC8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D86F7997945CD6FCE108F0E78720768BA4117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC60CDF180582EB8FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18BDFBBEFFF4125B51D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269176DF2183F8FC7C088D2E8BEBF93D4B068655334FD4449CB33AC447995A7AD186D8C47C27EEC5E9FD5E8D9A59859A8B65B4C35AC65A386B3089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A50462D7E8FABF96E53C5B79C489582AA4833CFFA219312D73F87CCE6106E1FC07E67D4AC08A07B9B06A1CB4668A9CA5FACB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADBF74143AD284FC7177DD89D51EBB7742424CF958EAFF5D571004E42C50DC4CA955A7F0CF078B5EC49A30900B95165D34D95739AEDB3821B435AEF746F4C39B5F40AC6654504BECDDDC48E490BE9136F5153D51B2EB5221511D7E09C32AA3244CE07F7565CF878F50073C76D6D5E79ED9CE0B41342B755BCDBAD658CF5C8AB4025DA084F8E80FEBD3202CD0F03380D9577A83BD0C44CE203720ABEDE4BBDD9CDD X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojEbds4VBqihQffx5h76U8aw== X-DA7885C5: 41A28F30EE74B29FEC3CE2FA85F464D007F89AE744E45300F216BC85171843AA262E2D401490A4A0DB037EFA58388B346E8BC1A9835FDE71 X-Mailru-Sender: 689FA8AB762F73930F533AC2B33E986B10907476DA10405178A2CD9E5BCE992E0FBE9A32752B8C9C2AA642CC12EC09F1FB559BB5D741EB962F61BD320559CF1EFD657A8799238ED55FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit][v2] LJ_GC64: Fix lua_concat(). X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: max.kokryashkin@gmail.com, tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Sergey! Thanks for the patch! LGTM, after fixing a few nits mentioned below. On 06.10.23, Sergey Bronnikov wrote: > From: Sergey Bronnikov > > Reported by Mathias Westerdahl. > > (cherry picked from commit 633f265f67f322cbe2c5fd11d3e46d968ac220f7) > > Lua 5.1 Reference Manual [1] defines a function `lua_concat`, that: > > > void lua_concat (lua_State *L, int n); > > > > Concatenates the n values at the top of the stack, pops them, and leaves > > the result at the top. > > Without the patch `lua_concat()` behaved incorrectly with userdata with Typo: s/patch/patch,/ > defined `__concat` metamethod. The problem is GC64-specific. Typo: s/defined/the defined/ > > Assuming we have three literals and a userdata with defined "__concat" Typo: s/defined/the defined/ > metamethod on top of the Lua stack: > > 1 [string] > 2 [string] > 3 [string] > 4 [string] > 5 [userdata] <--- top > > On attempt to concatenate *two* items on top of the Lua stack, > `lua_concat()` concatenates *four* items and leaves result on the top: Typo: s/result/the result/ Typo: s/the top/top of the Lua stack/ or s/on the top/at the top of the Lua stack/ > > 1 [string] > 2 [string][string][string][userdata] <--- top > > The problem is in incorrect calculation of `n` counter in the loop in Typo: s/in incorrect/in the incorrect/ > implementation of function `lua_concat`. Without the fix `n` is equal to Typo: s/in implementation/in the implementation/ Typo: s/fix/fix,/ > 3 at the end of the first iteration and therefore it goes to the next Typo: s/ and/, and/ > iteration of concatenation. In the fixed implementation of > `lua_concat()` `n` is equal to 1 at the end of the first loop iteration, Typo: s/`lua_concat()`/`lua_concat()`,/ > decremented in a loop postcondition and breaks the loop. Typo: s/ and/, and/ > Minor: Also, we can mention that for details, the reader can see `lj_meta_cat()` implementation in . Feel free to ignore. > The patch fixes incorrect behaviour. > > 1. https://www.lua.org/manual/5.1/manual.html > > Sergey Bronnikov: > * added the description and the test for the problem > > Part of tarantool/tarantool#8825 I suppose this should be #9145. > --- > PR: https://github.com/tarantool/tarantool/pull/9176 > Branch: https://github.com/tarantool/luajit/commits/ligurio/lj-881-fix-concat > Issues: > - https://github.com/LuaJIT/LuaJIT/issues/881 > - https://github.com/tarantool/tarantool/issues/8825 > 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 > new file mode 100644 > index 00000000..1b1f146f > --- /dev/null > +++ b/test/tarantool-c-tests/lj-881-fix-lua-concat.test.c > @@ -0,0 +1,95 @@ > +#include > + > +#include "lua.h" > +#include "lauxlib.h" > + > +#include "test.h" > +#include "utils.h" > + > +/* > + * This test demonstrates LuaJIT's incorrect behaviour, when Typo: s/,// > + * calling `lua_concat()` with userdata with __concat metamethod. Typo: s/__concat/the __concat/ > + * See https://github.com/LuaJIT/LuaJIT/issues/881 for details. > + */ > + > +#define MT_NAME "int" > + > +static int INFO_concat(lua_State *L) > +{ > + const char *s = luaL_checkstring(L, 1); > + int *n = (int *)luaL_checkudata(L, 2, MT_NAME); > + /* Do non-default concatenation. */ > + lua_pushfstring(L, "%s + %d", s, *n); > + return 1; > +} > + > +static const luaL_Reg mt[] = > +{ Minor: I suppose this line may be joined with the previous one, as far as this is not a function's body. > + {"__concat", INFO_concat}, Typo: s/, /, / > + {NULL, NULL} > +}; > + > +static int lua_concat_testcase(void *test_state) > +{ > + > + int *n = (int *)lua_newuserdata(L, sizeof(int)); I suggest to use `sizeof(*n)` here to avoid type duplication. > + *n = 100; I suppose we can use something like the following: | #define TEST_VALUE 100 | #define STR(s) #s | #define XSTR(s) STR(s) | #define TEST_VALUE_STR XSTR(TEST_VALUE) or just use #TEST_VALUE in the `expected_str[]` declaration. This helps to avoid the duplication of the magic number `100`. > + > + const char expected_str[] = "A + 100"; > + assert_str_equal(str, expected_str, strlen(expected_str)); > -- > 2.34.1 > -- Best regards, Sergey Kaplun