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 05000644CFC; Tue, 3 Oct 2023 18:35:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 05000644CFC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1696347318; bh=KsjKSlPYkvFfnH6bBuUKDL9814nGUkAiloB5mmGOWXU=; 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=QTFpObtYDkNa0vejxw8TrScVW1BoClvrQ+iIZyXb9ytRHVmg9liYyxZ8PDEvCsRIL HrGlbyWx1oYJNt8xyfPTWyCBqzuj9lEDJ9OryK0fSdCdY+iMLtRivexe46pPXJG2nv /myfbqmYAE/tznKLMm2GI5iOVeRc8nKx23C65WNk= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 CEF37644CFC for ; Tue, 3 Oct 2023 18:35:16 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CEF37644CFC Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1qnhQa-0007yf-0q; Tue, 03 Oct 2023 18:35:16 +0300 Message-ID: <2e683729-401b-1b86-5a60-54a2cd39dc84@tarantool.org> Date: Tue, 3 Oct 2023 18:35:15 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Content-Language: en-US To: Maxim Kokryashkin , Sergey Bronnikov Cc: max.kokryashkin@gmail.com, tarantool-patches@dev.tarantool.org References: <26636e3dcb8ade3bdc23d9544f9a7e2ffe4322b2.1695711374.git.sergeyb@tarantool.org> 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: 4F1203BC0FB41BD94A796AF4779ECFCE2FE4B73CD06F0B7CC57781C9F63FAA84182A05F5380850404D6B59E42465BE75E7423EE983E9A5B2C139E7678C6EDAAA1F4F876885AE16B2 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7B264C8851FD8E810EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637B103A303C52566448638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8002FA2E2B0DC626F40F3847E839A9F34117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCECADA55FE5B58BB7A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18F04B652EEC242312D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6F6B3B5B76D0D00FF089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5B3AED79113B7DD6A45030DF1507D39FA058869D06ED57F52F87CCE6106E1FC07E67D4AC08A07B9B067F1C1C3ABB44F3ACB5012B2E24CD356 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF3FED46C3ACD6F73ED3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CFC65B652ADB53F1E5262AA592F67A6862220412E254C828F2F7B3F9C3EE786B12498A0925D8903450CC231B5D63B17C6BCA1BCE9FC1D1706BF9592927266FAB10A74DFFEFA5DC0E7F02C26D483E81D6BE0DBAE6F56676BC7117BB6831D7356A2DEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojH0UbTrDsBkD/BqJQY13HnQ== X-DA7885C5: FD164A5A91509C1371BF151ACFF2D904718CDD875428F24EF917C2E8BF39F2BE262E2D401490A4A0DB037EFA58388B346E8BC1A9835FDE71 X-Mailru-Sender: 689FA8AB762F73930F533AC2B33E986B66E0475389ED68A6F9C74AFFA13BE937DD788429FD8613638ED9BB8B05EE7B3AFB559BB5D741EB96D19CD4E7312BAA970A04DAD6CC59E3365FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH luajit] 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, Max thanks for review, see my comments below Sergey On 9/29/23 11:24, Maxim Kokryashkin via Tarantool-patches wrote: > Hi, Sergey! > Thanks for the patch! > Please consider my comments below. > On Tue, Sep 26, 2023 at 09:56:31AM +0300, Sergey Bronnikov via Tarantool-patches 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 >> defined `__concat` metamethod. The problem is GC64-specific. >> >> Assuming we have three literals and a userdata with defined "__concat" >> 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 Lua stack, `lua_concat()` > Typo: s/on top/on top of the/ Fixed. >> concatenates *four* items and leaves result at the top: > Typo: s/at the/on the/ Fixed. >> 1 [string] >> 2 [string][string][string][userdata] <--- top >> >> The problem is in incorrect calculation of `n` counter in a loop in > Typo: s/in a loop/in the loop/ Fixed. >> implementation of function `lua_concat`. Without a fix `n` is equal to 3 > Typo: s/a fix/the fix/ Fixed. >> at the end of the first iteration and therefore it goes to the next >> iteration of concatenation. In a fixed implementation of `lua_concat()` > Typo: s/In a fixed/In the fixed/ Fixed. >> `n` is equal to 1 at the end of the first loop iteration, decremented in >> a loop postcondition and breaks a loop. > Typo: s/a loop/the loop/ Fixed. because no errors, typos, objections were found >> + >> +static const luaL_Reg INFO_methods[] = >> +{ >> + {0, 0} > Nit: It is more common to write `NULL` as sentinel. Fixed. >> +}; > This table is redundant. Fixed. >> + >> +static const luaL_Reg INFO_meta[] = >> +{ >> + {"__concat", INFO_concat}, >> + {0, 0} > Nit: It is more common to write `NULL` as sentinel. Fixed too. >> +}; >> + >> +static int lua_concat_testcase(void *test_state) >> +{ >> + lua_State *L = test_state; >> + >> + /* Create methods table, add it to the globals. */ >> + luaL_register(L, TYPE_NAME_INFO, INFO_methods); >> + int methods_idx = lua_gettop(L); > The methods table is redundant. Fixed as well. >> + /* Create metatable and add it to the Lua registry. */ >> + luaL_newmetatable(L, TYPE_NAME_INFO); >> + >> + int metatable_idx = lua_gettop(L); >> + /* Fill metatable. */ >> + luaL_register(L, 0, INFO_meta); > The middle argument is meant to be NULL. Sure, fixed. >> + >> + lua_pushliteral(L, "__metatable"); >> + /* Duplicate methods table. */ >> + lua_pushvalue(L, methods_idx); >> + lua_settable(L, metatable_idx); >> + lua_pop(L, 2); > This whole part can be reduced to this: > | /* Create metatable and add it to the Lua registry. */ > | luaL_newmetatable(L, TYPE_NAME_INFO); > | > | /* Fill metatable. */ > | luaL_register(L, 0, INFO_meta); > | lua_pop(L, 1); > | Reduced,  thanks. > >> + >> + assert_true(lua_gettop(L) == 0); >> + >> + Info info; >> + info.value = 7; >> + >> + lua_pushliteral(L, "C"); >> + lua_pushliteral(L, "B"); >> + lua_pushliteral(L, "A"); >> + lua_pushinfo(L, &info); >> + >> + int top = 4; >> + assert_true(lua_gettop(L) == top); >> + >> + /** >> + * 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. If n is 1, >> + * the result is the single value on the stack; if n is 0, >> + * the result is the empty string [1]. >> + * >> + * 1. https://www.lua.org/manual/5.1/manual.html >> + */ >> + int n = 2; >> + lua_concat(L, n); >> + >> + const char *str = lua_tostring(L, -1); >> + assert_true(top - n + 1 == lua_gettop(L)); > We have `assert_int_equal` in `test.h` for that. Sure, added in a separate patch and updated branch force-pushed. commit 71f4161115c2e2c13c91e2804f9c41f673ada5ce Author: Sergey Bronnikov Date:   Tue Oct 3 17:39:48 2023 +0300     test: introduce asserts assert_str{_not}_equal     The patch follows up commit a0483bd214f2 ("test: introduce module for C     tests") and adds additional asserts suitable for comparing strings. diff --git a/test/tarantool-c-tests/README.md b/test/tarantool-c-tests/README.md index 462534be..8fad6407 100644 --- a/test/tarantool-c-tests/README.md +++ b/test/tarantool-c-tests/README.md @@ -35,6 +35,8 @@ glibc `assert()`:    equal to the `b`.  * `assert_double{_not}_equal(a, b)` -- check that two doubles are {not}    **exactly** equal. +* `assert_str{_not}_equal(a, b)` -- check that `char *` variable `a` is {not} +  equal to the `b`.  ## Directives diff --git a/test/tarantool-c-tests/test.h b/test/tarantool-c-tests/test.h index 8b14c705..bbf573b2 100644 --- a/test/tarantool-c-tests/test.h +++ b/test/tarantool-c-tests/test.h @@ -13,8 +13,6 @@   * * Helpers assert macros:   *   - assert_uint_equal if needed   *   - assert_uint_not_equal if needed - *   - assert_str_equal if needed - *   - assert_str_not_equal if needed   *   - assert_memory_equal if needed   *   - assert_memory_not_equal if needed   * * Pragmas. @@ -214,4 +212,19 @@ static inline int todo(const char *reason)      );                                \  } while (0) +#define assert_str_equal(got, expected, n) do {                \ +    assert_general(strncmp(got, expected, n) == 0,            \ +               ASSERT_EQUAL_FMT(int, "%d"),            \ +               __FILE__, __LINE__, (got), (expected)        \ +    );                                \ +} while (0) + +#define assert_str_not_equal(got, unexpected, n) do { \ +    assert_general(strncmp(got, expected, n) != 0,            \ +               ASSERT_NOT_EQUAL_FMT(int, "%d"),            \ +               __FILE__, __LINE__, (got), (unexpected)        \ +    );                                \ +} while (0) + +  #endif /* TARANTOOL_LUAJIT_TEST_H */ >> + assert_true(strcmp(str, "A[Info.value=7]") == 0); > One should never use any of the unrestricted string functions, as > they are unsafe. Always use the `n`-limited versions: `strncmp` > instead of `strcmp`, `strnlen` instead of `strlen`, etc. > > Side note: there is a TODO in test.h about `assert_str_equal`, > maybe it's a great time to introduce it in this patch. Ok, replaced with strncmp. > >> + >> + /* Cleanup. */ >> + lua_settop(L, 0); >> + >> + return TEST_EXIT_SUCCESS; >> +} > Some general comments about that function: > 1. Please mark variables as `const` where possible. Done. > 2. C style requires you to put variable declarations at the > top of the function. Done. > 3. You have three distinct parts in this function: set up procedure, > the test itself, and the the tear down. It would be better to split them > into three separate functions to improve readability. Added a comments for splitting sections visually. >> + >> +int main(void) >> +{ >> + lua_State *L = utils_lua_init(); >> + const struct test_unit tgroup[] = { >> + test_unit_def(lua_concat_testcase), >> + }; >> + const int test_result = test_run_group(tgroup, L); >> + utils_lua_close(L); >> + >> + return test_result; >> +} >> -- >> 2.34.1 >> > Best regards, > Maxim Kokryashkin