From 656758c86ad929b07bb6422eb0f44cf9f2347aac Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 24 Apr 2017 12:01:42 +0200 Subject: Use maximum 32bit number instead of Number.MAX_SAFE_INTEGER --- src/llex.js | 5 +++-- src/llimit.js | 15 ++++++++++++--- src/lmathlib.js | 6 +++--- src/lobject.js | 5 +++-- src/loslib.js | 3 ++- src/lparser.js | 4 ++-- src/lstrlib.js | 3 ++- src/ltablib.js | 4 ++-- src/lua.js | 3 ++- src/luaconf.js | 4 +++- src/lutf8lib.js | 3 ++- 11 files changed, 36 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/llex.js b/src/llex.js index b038619..0927fec 100644 --- a/src/llex.js +++ b/src/llex.js @@ -9,6 +9,7 @@ const ldo = require('./ldo.js'); const ljstype = require('./ljstype'); const lobject = require('./lobject'); const lua = require('./lua.js'); +const llimit = require('./llimit.js'); const TValue = lobject.TValue; const CT = lua.constant_types; const TS = lua.thread_status; @@ -151,7 +152,7 @@ class LexState { const save = function(ls, c) { let b = ls.buff; if (b.n + 1 > b.buffer.length) { - if (b.buffer.length >= Number.MAX_SAFE_INTEGER/2) + if (b.buffer.length >= llimit.MAX_INT/2) lexerror(ls, lua.to_luastring("lexical element too long", true), 0); } b.buffer[b.n++] = c < 0 ? 255 + c + 1 : c; @@ -192,7 +193,7 @@ const inclinenumber = function(ls) { next(ls); /* skip '\n' or '\r' */ if (currIsNewline(ls) && ls.current !== old) next(ls); /* skip '\n\r' or '\r\n' */ - if (++ls.linenumber >= Number.MAX_SAFE_INTEGER) + if (++ls.linenumber >= llimit.MAX_INT) lexerror(ls, lua.to_luastring("chunk has too many lines", true), 0); }; diff --git a/src/llimit.js b/src/llimit.js index 9c02eb0..06b8370 100644 --- a/src/llimit.js +++ b/src/llimit.js @@ -1,6 +1,15 @@ /*jshint esversion: 6 */ "use strict"; -module.exports.LUAI_MAXCCALLS = 200; -module.exports.LUA_MAXINTEGER = 2147483647; -module.exports.LUA_MININTEGER = -2147483647; \ No newline at end of file +const LUAI_MAXCCALLS = 200; +module.exports.LUAI_MAXCCALLS = LUAI_MAXCCALLS; +const LUA_MAXINTEGER = 2147483647; +module.exports.LUA_MAXINTEGER = LUA_MAXINTEGER; +const LUA_MININTEGER = -2147483647; +module.exports.LUA_MININTEGER = LUA_MININTEGER; + +// If later integers are more than 32bit, LUA_MAXINTEGER will then be != MAX_INT +const MAX_INT = 2147483647; +module.exports.MAX_INT = MAX_INT; +const MIN_INT = -2147483647; +module.exports.MIN_INT = MIN_INT; diff --git a/src/lmathlib.js b/src/lmathlib.js index c052fcb..fd18711 100644 --- a/src/lmathlib.js +++ b/src/lmathlib.js @@ -42,7 +42,7 @@ const math_random = function(L) { /* random integer in the interval [low, up] */ lauxlib.luaL_argcheck(L, low <= up, 1, lua.to_luastring("interval is empty", true)); - lauxlib.luaL_argcheck(L, low >= 0 || up <= Number.MAX_SAFE_INTEGER + low, 1, + lauxlib.luaL_argcheck(L, low >= 0 || up <= llimit.MAX_INT + low, 1, lua.to_luastring("interval too large", true)); r *= (up - low) + 1; @@ -268,9 +268,9 @@ const luaopen_math = function(L) { lapi.lua_setfield(L, -2, lua.to_luastring("pi", true)); lapi.lua_pushnumber(L, Number.MAX_VALUE); lapi.lua_setfield(L, -2, lua.to_luastring("huge", true)); - lapi.lua_pushinteger(L, Number.MAX_SAFE_INTEGER); + lapi.lua_pushinteger(L, llimit.MAX_INT); lapi.lua_setfield(L, -2, lua.to_luastring("maxinteger", true)); - lapi.lua_pushinteger(L, Number.MIN_SAFE_INTEGER); + lapi.lua_pushinteger(L, llimit.MIN_INT); lapi.lua_setfield(L, -2, lua.to_luastring("mininteger", true)); return 1; }; diff --git a/src/lobject.js b/src/lobject.js index 9a79c95..7409415 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -6,6 +6,7 @@ const assert = require('assert'); const ljstype = require('./ljstype.js'); const lua = require('./lua.js'); const luaconf = require('./luaconf.js'); +const llimit = require('./llimit.js'); const CT = lua.constant_types; const UpVal = require('./lfunc.js').UpVal; const char = lua.char; @@ -427,8 +428,8 @@ const l_str2d = function(s) { return end; }; -const MAXBY10 = Number.MAX_SAFE_INTEGER / 10; -const MAXLASTD = Number.MAX_SAFE_INTEGER % 10; +const MAXBY10 = llimit.MAX_INT / 10; +const MAXLASTD = llimit.MAX_INT % 10; const l_str2int = function(s) { let a = 0; diff --git a/src/loslib.js b/src/loslib.js index a319b3b..781dec9 100644 --- a/src/loslib.js +++ b/src/loslib.js @@ -7,6 +7,7 @@ const char = lua.char; const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); const ldebug = require('./ldebug.js'); +const llimit = require('./llimit.js'); const setfield = function(L, key, value) { lapi.lua_pushinteger(L, value); @@ -26,7 +27,7 @@ const setallfields = function(L, time) { // setboolfield(L, "isdst", time.get); }; -const L_MAXDATEFIELD = (Number.MAX_SAFE_INTEGER / 2); +const L_MAXDATEFIELD = (llimit.MAX_INT / 2); const getfield = function(L, key, d, delta) { let t = lapi.lua_getfield(L, -1, lua.to_luastring(key)); /* get field and its type */ diff --git a/src/lparser.js b/src/lparser.js index a0d671b..fba5f94 100644 --- a/src/lparser.js +++ b/src/lparser.js @@ -638,7 +638,7 @@ const recfield = function(ls, cc) { let val = new expdesc(); if (ls.t.token === R.TK_NAME) { - checklimit(fs, cc.nh, Number.MAX_SAFE_INTEGER, lua.to_luastring("items in a constructor", true)); + checklimit(fs, cc.nh, llimit.MAX_INT, lua.to_luastring("items in a constructor", true)); checkname(ls, key); } else /* ls->t.token === '[' */ yindex(ls, key); @@ -676,7 +676,7 @@ const lastlistfield = function(fs, cc) { const listfield = function(ls, cc) { /* listfield -> exp */ expr(ls, cc.v); - checklimit(ls.fs, cc.na, Number.MAX_SAFE_INTEGER, lua.to_luastring("items in a constructor", true)); + checklimit(ls.fs, cc.na, llimit.MAX_INT, lua.to_luastring("items in a constructor", true)); cc.na++; cc.tostore++; }; diff --git a/src/lstrlib.js b/src/lstrlib.js index f8158a8..f26400e 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -8,6 +8,7 @@ const lauxlib = require('./lauxlib.js'); const lobject = require('./lobject.js'); const lua = require('./lua.js'); const luaconf = require('./luaconf.js'); +const llimit = require('./llimit.js'); const CT = lua.constant_types; const char = lua.char; @@ -682,7 +683,7 @@ const str_byte = function(L) { if (posi < 1) posi = 1; if (pose > l) pose = l; if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= Number.MAX_SAFE_INTEGER) /* arithmetic overflow? */ + if (pose - posi >= llimit.MAX_INT) /* arithmetic overflow? */ return lauxlib.luaL_error(L, lua.to_luastring("string slice too long", true)); let n = (pose - posi) + 1; diff --git a/src/ltablib.js b/src/ltablib.js index 0794887..bc51836 100644 --- a/src/ltablib.js +++ b/src/ltablib.js @@ -172,7 +172,7 @@ const unpack = function(L) { let e = lauxlib.luaL_opt(L, lauxlib.luaL_checkinteger, 3, lauxlib.luaL_len(L, 1)); if (i > e) return 0; /* empty range */ let n = e - i; /* number of elements minus 1 (avoid overflows) */ - if (n >= Number.MAX_SAFE_INTEGER || !lapi.lua_checkstack(L, ++n)) + if (n >= llimit.MAX_INT || !lapi.lua_checkstack(L, ++n)) return lauxlib.luaL_error(L, lua.to_luastring("too many results to unpack", true)); for (; i < e; i++) /* push arg[i..e - 1] (to avoid overflows) */ lapi.lua_geti(L, 1, i); @@ -214,7 +214,7 @@ const auxsort = function(L) { const sort = function(L) { let n = aux_getn(L, 1, TAB_RW); if (n > 1) { /* non-trivial interval? */ - lauxlib.luaL_argcheck(L, n < Number.MAX_SAFE_INTEGER, 1, lua.to_luastring("array too big", true)); + lauxlib.luaL_argcheck(L, n < llimit.MAX_INT, 1, lua.to_luastring("array too big", true)); if (!lapi.lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ lauxlib.luaL_checktype(L, 2, CT.LUA_TFUNCTION); /* must be a function */ lapi.lua_settop(L, 2); /* make sure there are two arguments */ diff --git a/src/lua.js b/src/lua.js index 5175bfb..e1366b0 100644 --- a/src/lua.js +++ b/src/lua.js @@ -3,6 +3,7 @@ const assert = require('assert'); const luaconf = require('./luaconf.js'); +const llimit = require('./llimit.js'); // To avoid charCodeAt everywhere const char = []; @@ -150,7 +151,7 @@ const to_luastring = function(str, cache, maxBytesToWrite) { if (Array.isArray(cached)) return cached; } - maxBytesToWrite = maxBytesToWrite !== undefined ? maxBytesToWrite : Number.MAX_SAFE_INTEGER; + maxBytesToWrite = maxBytesToWrite !== undefined ? maxBytesToWrite : llimit.MAX_INT; let outU8Array = []; if (!(maxBytesToWrite > 0)) // Parameter maxBytesToWrite is not optional. Negative values, 0, null, undefined and false each don't write out any bytes. diff --git a/src/luaconf.js b/src/luaconf.js index 3314d5c..803bb90 100644 --- a/src/luaconf.js +++ b/src/luaconf.js @@ -1,6 +1,8 @@ /*jshint esversion: 6 */ "use strict"; +const llimit = require('./llimit.js'); + /* @@ LUAI_MAXSTACK limits the size of the Lua stack. ** CHANGE it if you need a different limit. This limit is arbitrary; @@ -17,7 +19,7 @@ const LUAI_MAXSTACK = 1000000; const LUA_IDSIZE = 60; const lua_numbertointeger = function(n) { - return n >= Number.MIN_SAFE_INTEGER && n < -Number.MIN_SAFE_INTEGER ? n : 0; + return n >= llimit.MIN_INT && n < -llimit.MIN_INT ? n : 0; }; const LUA_INTEGER_FRMLEN = ""; diff --git a/src/lutf8lib.js b/src/lutf8lib.js index f58223a..9d53a8c 100644 --- a/src/lutf8lib.js +++ b/src/lutf8lib.js @@ -5,6 +5,7 @@ const assert = require('assert'); const lua = require('./lua.js'); const lapi = require('./lapi.js'); const lauxlib = require('./lauxlib.js'); +const llimit = require('./llimit.js'); const MAXUNICODE = 0x10FFFF; @@ -170,7 +171,7 @@ const codepoint = function(L) { lauxlib.luaL_argcheck(L, pose <= s.length, 3, lua.to_luastring("out of range", true)); if (posi > pose) return 0; /* empty interval; return no values */ - if (pose - posi >= Number.MAX_SAFE_INTEGER) + if (pose - posi >= llimit.MAX_INT) return lauxlib.luaL_error(L, lua.to_luastring("string slice too long", true)); let n = (pose - posi) + 1; lauxlib.luaL_checkstack(L, n, lua.to_luastring("string slice too long", true)); -- cgit v1.2.3-70-g09d2