forked from LuaJIT/LuaJIT
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit exponent range in number parsing.
Reported by XmiliaH. (cherry-picked from commit e560487) 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
- Loading branch information
Showing
2 changed files
with
33 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
test/tarantool-tests/lj-788-limit-exponents-range.test.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <src/lj_strscan.c> 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) |