From deac067ed39a44c001599c0d15de09872496b2aa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 22 Sep 2021 13:10:39 -0300 Subject: Avoid overflows when incrementing parameters in C Any C function can receive maxinteger as an integer argument, and therefore cannot increment it without some care (e.g., doing unsigned arithmetic as the core does). --- testes/nextvar.lua | 17 +++++++++++++++++ testes/utf8.lua | 6 ++++++ 2 files changed, 23 insertions(+) (limited to 'testes') diff --git a/testes/nextvar.lua b/testes/nextvar.lua index 076f6361..9e23e572 100644 --- a/testes/nextvar.lua +++ b/testes/nextvar.lua @@ -43,6 +43,14 @@ assert(i == 4) assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{}) +do -- overflow (must wrap-around) + local f = ipairs{} + local k, v = f({[math.mininteger] = 10}, math.maxinteger) + assert(k == math.mininteger and v == 10) + k, v = f({[math.mininteger] = 10}, k) + assert(k == nil) +end + if not T then (Message or print) ('\n >>> testC not active: skipping tests for table sizes <<<\n') @@ -499,6 +507,15 @@ do -- testing table library with metamethods end +do -- testing overflow in table.insert (must wrap-around) + + local t = setmetatable({}, + {__len = function () return math.maxinteger end}) + table.insert(t, 20) + local k, v = next(t) + assert(k == math.mininteger and v == 20) +end + if not T then (Message or print) ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n') diff --git a/testes/utf8.lua b/testes/utf8.lua index 6010d1ad..461e223c 100644 --- a/testes/utf8.lua +++ b/testes/utf8.lua @@ -112,6 +112,12 @@ do end errorcodes("ab\xff") errorcodes("\u{110000}") + + -- calling interation function with invalid arguments + local f = utf8.codes("") + assert(f("", 2) == nil) + assert(f("", -1) == nil) + assert(f("", math.mininteger) == nil) end -- error in initial position for offset -- cgit v1.2.3-55-g6feb