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 A40CA26A30A; Wed, 1 Feb 2023 10:50:23 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A40CA26A30A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1675237823; bh=RZPqp+henY1NspJOBgVCpIJAeilkHlai37pRjhhb3M8=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=gwX6yvnO/PUPTTMoaNdpxPvbR8PJSPsTiOOskYHzGCRhV9TD99lwOFzT3SJIXfeEL FFiowfdErKNoyMIaUuw43sVlkeiGJz76ofarke/nrblKJKqJ4WYma4/dv4o0q/fReo cTZwHjKnj4a2zGGSlXtSYcBwGnmX4/NVnZ0YQyGY= 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 07EA47F7C1 for ; Wed, 1 Feb 2023 10:50:22 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 07EA47F7C1 Received: by smtpng3.m.smailru.net with esmtpa (envelope-from ) id 1pN7sq-0006B6-Mk; Wed, 01 Feb 2023 10:50:21 +0300 To: Sergey Ostanevich , Maxim Kokryashkin Date: Wed, 1 Feb 2023 10:46:51 +0300 Message-Id: <20230201074651.8282-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD939F4CB9F411D0C04563E6DE9EA85E9752C3C2CBFE51DBF69182A05F5380850408080148E16678685EF281A12ADCEFAA5879DD4782CA461D820D4A79971F1591D X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7544B1CCE26E01C74EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637BC9F889FE9D346E88638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8B2F0D8C80F054E578C7D083C1DD9BAEA117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC1BE95B8C87527B4BA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F4460429728776938767073520C65AC60A1F0286FEF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BAEB924C2B054B06E75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A569CE90056F9B503F4B91CA616CE6009D91C49943A9188FE14EAF44D9B582CE87C8A4C02DF684249CC203C45FEA855C8F X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34A533D6DF7731C6642F4FE98AA638C952DEDA1BFAB8C28C29FDA0797BECBC1A691704035FECCE50EE1D7E09C32AA3244C1A9A2DFF14F1D97AE3796C37D9E026F3259227199D06760A927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojUFx0YIIX6pwwoxXqAnEKhg== X-DA7885C5: A6EA994F53899314C0228B55B2F3A323CA8399E2A50BAE8C5F9DFFFBACCFA4F1262E2D401490A4A0DB037EFA58388B346E8BC1A9835FDE71 X-Mailru-Sender: 689FA8AB762F73933AF1F914F131DBF59B1AE19E368F3F3DDD497CA5D86DD8E70FBE9A32752B8C9C2AA642CC12EC09F1FB559BB5D741EB962F61BD320559CF1EFD657A8799238ED55FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Fix os.date() for wider libc strftime() compatibility. 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: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall Thanks to Jesper Lundgren. (cherry picked from commit fc63c938b522e147ea728b75f385728bf4a8fc35) When `strftime()` returns empty result (for example, for "%p" on some locales or when LuaJIT is built with musl's `strftime()` and format string is invalid), the `os.date()` endless retries to format result and reallocates buffer for resulting string. This leads to OOM. This patch limits amount of retries by 4. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#8069 --- PR: https://github.com/tarantool/tarantool/pull/8237 Issues: * https://github.com/LuaJIT/LuaJIT/issues/463 * https://github.com/tarantool/tarantool/issues/8069 Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-463-os-date-oom-full-ci src/lib_os.c | 4 ++-- .../lj-463-os-date-oom.test.lua | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 test/tarantool-tests/lj-463-os-date-oom.test.lua diff --git a/src/lib_os.c b/src/lib_os.c index 9e78d49a..ffbc3fdc 100644 --- a/src/lib_os.c +++ b/src/lib_os.c @@ -205,12 +205,12 @@ LJLIB_CF(os_date) setboolfield(L, "isdst", stm->tm_isdst); } else if (*s) { SBuf *sb = &G(L)->tmpbuf; - MSize sz = 0; + MSize sz = 0, retry = 4; const char *q; for (q = s; *q; q++) sz += (*q == '%') ? 30 : 1; /* Overflow doesn't matter. */ setsbufL(sb, L); - for (;;) { + while (retry--) { /* Limit growth for invalid format or empty result. */ char *buf = lj_buf_need(sb, sz); size_t len = strftime(buf, sbufsz(sb), s, stm); if (len) { diff --git a/test/tarantool-tests/lj-463-os-date-oom.test.lua b/test/tarantool-tests/lj-463-os-date-oom.test.lua new file mode 100644 index 00000000..cce78b6e --- /dev/null +++ b/test/tarantool-tests/lj-463-os-date-oom.test.lua @@ -0,0 +1,19 @@ +local tap = require('tap') + +-- See also https://github.com/LuaJIT/LuaJIT/issues/463. +local test = tap.test('lj-463-os-date-oom') +test:plan(1) + +-- The ru_RU.utf8 locale is chosen as one that will be set on a +-- developer's PC with high possibility. It may be unavailable at +-- CI, so don't check the status and result of function calls. +-- If it's unavailable `os.setlocale()` does nothing. +-- Before the patch, the call to `os.date('%p')` on non-standard +-- locale may lead to OOM. + +os.setlocale('ru_RU.utf8') +os.date('%p') + +test:ok(true, 'os.date() finished without OOM') + +os.exit(test:check() and 0 or 1) -- 2.34.1