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 EDE0B6E40C4; Tue, 21 Nov 2023 11:57:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org EDE0B6E40C4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1700557049; bh=oKpURaniP2Oar65xLN4ptgrQO9MeRsvfbPlxjLBY5CA=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=dYSQ+i5dOTfEhvOM3UTBtKj94aB4iAvlD1gN1VsTHG8ljaZqgip7ldweAFKSsg+ON Jau3xvayPBmnOnTM595JfOUzUUjBwvxbilaUz9VooOzHJO7so8vj1KDz7YoRq4ZB/f q59NEvPvwQ4Du4JO72ZMFi93zzojvDoncef5VjhI= Received: from smtp54.i.mail.ru (smtp54.i.mail.ru [95.163.41.89]) (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 5B1CD597C28 for ; Tue, 21 Nov 2023 11:57:27 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5B1CD597C28 Received: by smtp54.i.mail.ru with esmtpa (envelope-from ) id 1r5MZR-00Gh0s-2l; Tue, 21 Nov 2023 11:57:26 +0300 To: Maxim Kokryashkin , Sergey Bronnikov Date: Tue, 21 Nov 2023 11:52:53 +0300 Message-ID: <20231121085253.13526-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.42.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-7564579A: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD93F1575C7510F5547926B4B9114735657696DFC1CFA9ECB9A00894C459B0CD1B91EFCCFDB3D8FC2219739826DA16B9456667380FC9915A4040F4B8A9FC37412E1 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76ABD3380F320B62CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637BA9119251D79ECE08638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D839DD87374628D90766501ADE667665DB117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC55B19328CBC4F849A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751FE5D25F19253116ADD2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE4B6963042765DA4BFCE65BE3358055BDD8FC6C240DEA76429C9F4D5AE37F343AA9539A8B242431040A6AB1C7CE11FEE3223C850C9476430903F1AB874ED89028C4224003CC836476E2F48590F00D11D6E2021AF6380DFAD1A18204E546F3947CD2DCF9CF1F528DBC2E808ACE2090B5E1725E5C173C3A84C3C5EA940A35A165FF2DBA43225CD8A89F0A35B161A8BF67C142539A7722CA490CB5C8C57E37DE458BEDA766A37F9254B7 X-C1DE0DAB: 0D63561A33F958A5E17BFECD8F09A43471875204BECD473DB605F01D5DF0DF3DF87CCE6106E1FC07E67D4AC08A07B9B0735DFC8FA7AC12079C5DF10A05D560A950611B66E3DA6D700B0A020F03D25A0997E3FB2386030E77 X-C8649E89: 1C3962B70DF3F0ADE00A9FD3E00BEEDF77DD89D51EBB7742D3581295AF09D3DF87807E0823442EA2ED31085941D9CD0AF7F820E7B07EA4CFBACE1FC1FC7EDA4C333E13E083C8806BE4BBC3AEE74DD9A653AB8163AEFC4BAB08FC8DA71D38548888AD3775A6B0C8340C90CF974D676857E72D9A49DA4C5ADBA74DFFEFA5DC0E7F02C26D483E81D6BE5EF9655DD6DEA7D65774BB76CC95456EEC5B5AD62611EEC62B5AFB4261A09AF0 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojib+6XWwA4NA6SUxgM0ev1g== X-Mailru-Sender: 11C2EC085EDE56FAC07928AF2646A769C6E284BD96F4603118EFB959142E4102FE5F7FEA15315F16DEDBA653FF35249392D99EB8CC7091A70E183A470755BFD208F19895AA18418972D6B4FCE48DF648AE208404248635DF X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Limit exponent range in number parsing. 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 Reported by XmiliaH. (cherry-picked from commit e56048753634c32ea6eeedf74cef6f9cfea5f4ed) When parsing exponent powers greater than (1 << 16) * 10 == (65536 * 10), the exponent values are cut without handling any values greater. This patch fixes the behaviour, but restricts the power maximum value by `STRSCAN_MAXEXP` (1 << 20). Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#9145 --- Branch: https://github.com/tarantool/luajit/tree/skaplun/lj-788-limit-exponents-range Tarantool PR: https://github.com/tarantool/tarantool/pull/9386 Related issues: * https://github.com/LuaJIT/LuaJIT/issues/788 * https://github.com/tarantool/tarantool/issues/9145 src/lj_strscan.c | 5 +++- .../lj-788-limit-exponents-range.test.lua | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/lj-788-limit-exponents-range.test.lua diff --git a/src/lj_strscan.c b/src/lj_strscan.c index ae8945e1..129010fd 100644 --- a/src/lj_strscan.c +++ b/src/lj_strscan.c @@ -63,6 +63,7 @@ #define STRSCAN_MAXDIG 800 /* 772 + extra are sufficient. */ #define STRSCAN_DDIG (STRSCAN_DIG/2) #define STRSCAN_DMASK (STRSCAN_DDIG-1) +#define STRSCAN_MAXEXP (1 << 20) /* Helpers for circular buffer. */ #define DNEXT(a) (((a)+1) & STRSCAN_DMASK) @@ -449,6 +450,7 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, MSize len, TValue *o, if (dig) { ex = (int32_t)(dp-(p-1)); dp = p-1; while (ex < 0 && *dp-- == '0') ex++, dig--; /* Skip trailing zeros. */ + if (ex <= -STRSCAN_MAXEXP) return STRSCAN_ERROR; if (base == 16) ex *= 4; } } @@ -462,7 +464,8 @@ StrScanFmt lj_strscan_scan(const uint8_t *p, MSize len, TValue *o, if (!lj_char_isdigit(*p)) return STRSCAN_ERROR; xx = (*p++ & 15); while (lj_char_isdigit(*p)) { - if (xx < 65536) xx = xx * 10 + (*p & 15); + xx = xx * 10 + (*p & 15); + if (xx >= STRSCAN_MAXEXP) return STRSCAN_ERROR; p++; } ex += negx ? -(int32_t)xx : (int32_t)xx; diff --git a/test/tarantool-tests/lj-788-limit-exponents-range.test.lua b/test/tarantool-tests/lj-788-limit-exponents-range.test.lua new file mode 100644 index 00000000..8ab31600 --- /dev/null +++ b/test/tarantool-tests/lj-788-limit-exponents-range.test.lua @@ -0,0 +1,29 @@ +local tap = require('tap') + +-- Test file to demonstrate incorrect behaviour of exponent number +-- form parsing. +-- See also: https://github.com/LuaJIT/LuaJIT/issues/788. +local test = tap.test('lj-788-limit-exponents-range') +test:plan(2) + +-- Before the patch, the powers greater than (1 << 16) * 10 +-- (655360) were parsed incorrectly. After the patch, powers +-- greater than 1 << 20 (1048576 `STRSCAN_MAXEXP`) are considered +-- invalid. See for details. +-- Choose the first value between these values and the second +-- value bigger than `STRSCAN_MAXEXP` to check parsing correctness +-- for the first one, and `STRSCAN_ERROR` for the second case. +local PARSABLE_EXP_POWER = 1000000 +local TOO_LARGE_EXP_POWER = 1050000 + +local function form_exp_string(n) + return '0.' .. string.rep('0', n - 1) .. '1e' .. tostring(n) +end + +test:is(tonumber(form_exp_string(PARSABLE_EXP_POWER)), 1, + 'correct parsing of large exponent') + +test:is(tonumber(form_exp_string(TOO_LARGE_EXP_POWER)), nil, + 'too big exponent power is not parsed') + +test:done(true) -- 2.42.1