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 6FEE26A5AF1; Tue, 10 Oct 2023 15:33:48 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6FEE26A5AF1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1696941228; bh=DYvzTf2aXr/t1ipyC/J3Ypxrg7VNij67SroWzEOH4Eo=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=l3hTtFSTZleHfioNWHTR7nS9N6oda484Em67WqFtbip21NJsKLuLY6ou/dC2CS7JB SiwY+kBz2y1LQAgylBhYgKV+tfjSfC9lZpKiQSu9ajUROqLAq2yMMud5l0UdRpwVYe etrKL7grq5UhTYgDIzz33zw8ST+GgxXPzTaneAuU= Received: from smtp35.i.mail.ru (smtp35.i.mail.ru [95.163.41.76]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 280B15042C1 for ; Tue, 10 Oct 2023 15:33:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 280B15042C1 Received: by smtp35.i.mail.ru with esmtpa (envelope-from ) id 1qqBvl-00D8ui-2t; Tue, 10 Oct 2023 15:33:46 +0300 Message-ID: Date: Tue, 10 Oct 2023 15:33:45 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: Sergey Kaplun , Sergey Bronnikov Cc: tarantool-patches@dev.tarantool.org, max.kokryashkin@gmail.com References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9FE0487E502468146151CFFDF2BBA58DC1B080ACA52AE241C00894C459B0CD1B9F924D3AE06BAE7DC503401C1B40899B78EED2E2684C4CCCF19F33B7625D3E01E X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE728F774C865CF4B07EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637BA14B6CBCF09CA1D8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8707554CF76C9AE12F84B935A73D2DA9F117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC60CDF180582EB8FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751FC26CFBAC0749D213D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EECCD848CCB6FE560CBDB03A3F2A65D472D8FC6C240DEA76429C9F4D5AE37F343AA9539A8B242431040A6AB1C7CE11FEE386D40F53BA19229503F1AB874ED89028C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947C4D3DDB508812D9002E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89FC0F9454058DFE53CCE5475246E174218B5C8C57E37DE458BEDA766A37F9254B7 X-C1DE0DAB: 0D63561A33F958A52EF7A903845B56385457E340CE48382233956902E76329D2F87CCE6106E1FC07E67D4AC08A07B9B062B3BD3CC35DA588CB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADBF74143AD284FC7177DD89D51EBB7742424CF958EAFF5D571004E42C50DC4CA955A7F0CF078B5EC49A30900B95165D341E2D05735FCBECD15C76CE80916E8C8CDB9DFB78852CB4E1C7D8A406C1CC5CEB246643B0E70770701D7E09C32AA3244CF591F006B7C83D24FF72FD0DACA1AEF9BBA718C7E6A9E042BAD658CF5C8AB4025DA084F8E80FEBD3FFA33E6B6B2F82C47A83BD0C44CE203720ABEDE4BBDD9CDD X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj7Bv9MRqw1O0ll7DIjeAUtA== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A76981C4FEB6E6573978E1121D9E95456EB84AF810104A35EE5EEBA65886582A37BD66FEC6BF5C9C28D98A98C1125256619760D574B6FC815AB872D6B4FCE48DF648AE208404248635DF 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 Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Sergey On 09.10.2023 17:11, Sergey Kaplun wrote: > 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,/ Fixed. > >> defined `__concat` metamethod. The problem is GC64-specific. > Typo: s/defined/the defined/ Fixed. > >> Assuming we have three literals and a userdata with defined "__concat" > Typo: s/defined/the defined/ Fixed. > >> 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/ Fixed. > >> 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/ Fixed. > >> implementation of function `lua_concat`. Without the fix `n` is equal to > Typo: s/in implementation/in the implementation/ > Typo: s/fix/fix,/ Fixed. > >> 3 at the end of the first iteration and therefore it goes to the next > Typo: s/ and/, and/ Fixed. > >> 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()`,/ Fixed. > >> decremented in a loop postcondition and breaks the loop. > Typo: s/ and/, and/ Fixed. > Minor: Also, we can mention that for details, the reader can see > `lj_meta_cat()` implementation in . > Feel free to ignore. Added. >> 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. Done. > >> --- >> 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/,// Fixed. > >> + * calling `lua_concat()` with userdata with __concat metamethod. > Typo: s/__concat/the __concat/ Fixed. > >> + * 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. Joined. > >> + {"__concat", INFO_concat}, > Typo: s/, /, / Fixed. > >> + {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. Fixed. > >> + *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`. Updated:  #define TYPE_NAME "int" +#define TEST_VALUE 100 +#define TOSTR(s) #s +#define CONCAT(A, B) A TOSTR(B) +  static int __concat(lua_State *L)  {         const char *s = luaL_checkstring(L, 1); @@ -73,7 +76,7 @@ static int lua_concat_testcase(void *test_state)         const char *str = lua_tostring(L, -1);         assert_int_equal(lua_gettop(L), top - 2 + 1); -       const char expected_str[] = "A + 100"; +       const char expected_str[] = CONCAT("A + ", TEST_VALUE);         assert_str_equal(str, expected_str, strlen(expected_str));         /* Teardown. */ > >> + > > >> + const char expected_str[] = "A + 100"; >> + assert_str_equal(str, expected_str, strlen(expected_str)); > > >> -- >> 2.34.1 >>