aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-09-22 13:10:39 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-09-22 13:10:39 -0300
commitdeac067ed39a44c001599c0d15de09872496b2aa (patch)
treed7373651e7d54a8ca5ffa4841379a4d9149164aa /testes
parent2ff34717227b8046b0fdcb96206f11f5e888664e (diff)
downloadlua-deac067ed39a44c001599c0d15de09872496b2aa.tar.gz
lua-deac067ed39a44c001599c0d15de09872496b2aa.tar.bz2
lua-deac067ed39a44c001599c0d15de09872496b2aa.zip
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).
Diffstat (limited to 'testes')
-rw-r--r--testes/nextvar.lua17
-rw-r--r--testes/utf8.lua6
2 files changed, 23 insertions, 0 deletions
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)
43assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{}) 43assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
44 44
45 45
46do -- overflow (must wrap-around)
47 local f = ipairs{}
48 local k, v = f({[math.mininteger] = 10}, math.maxinteger)
49 assert(k == math.mininteger and v == 10)
50 k, v = f({[math.mininteger] = 10}, k)
51 assert(k == nil)
52end
53
46if not T then 54if not T then
47 (Message or print) 55 (Message or print)
48 ('\n >>> testC not active: skipping tests for table sizes <<<\n') 56 ('\n >>> testC not active: skipping tests for table sizes <<<\n')
@@ -499,6 +507,15 @@ do -- testing table library with metamethods
499end 507end
500 508
501 509
510do -- testing overflow in table.insert (must wrap-around)
511
512 local t = setmetatable({},
513 {__len = function () return math.maxinteger end})
514 table.insert(t, 20)
515 local k, v = next(t)
516 assert(k == math.mininteger and v == 20)
517end
518
502if not T then 519if not T then
503 (Message or print) 520 (Message or print)
504 ('\n >>> testC not active: skipping tests for table library on non-tables <<<\n') 521 ('\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
112 end 112 end
113 errorcodes("ab\xff") 113 errorcodes("ab\xff")
114 errorcodes("\u{110000}") 114 errorcodes("\u{110000}")
115
116 -- calling interation function with invalid arguments
117 local f = utf8.codes("")
118 assert(f("", 2) == nil)
119 assert(f("", -1) == nil)
120 assert(f("", math.mininteger) == nil)
115end 121end
116 122
117-- error in initial position for offset 123-- error in initial position for offset