aboutsummaryrefslogtreecommitdiff
path: root/testes
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-03-10 15:21:32 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-03-10 15:21:32 -0300
commitb5b1995f2925b2f9be4a48304ac97a38f8608648 (patch)
tree6935ced2337eb900029ccb119636185df5037fd8 /testes
parentcb88c1cd5d22fe7c56f4f374ded7c16f7cf14bf3 (diff)
downloadlua-b5b1995f2925b2f9be4a48304ac97a38f8608648.tar.gz
lua-b5b1995f2925b2f9be4a48304ac97a38f8608648.tar.bz2
lua-b5b1995f2925b2f9be4a48304ac97a38f8608648.zip
Checks for type 'int' added to binary header
The structure 'AbsLineInfo' is hard-dumped into binary chunks, and it comprises two 'int' fields.
Diffstat (limited to 'testes')
-rw-r--r--testes/calls.lua47
1 files changed, 30 insertions, 17 deletions
diff --git a/testes/calls.lua b/testes/calls.lua
index 31028215..8b355957 100644
--- a/testes/calls.lua
+++ b/testes/calls.lua
@@ -480,15 +480,22 @@ assert((function (a) return a end)() == nil)
480 480
481print("testing binary chunks") 481print("testing binary chunks")
482do 482do
483 local header = string.pack("c4BBc6BBB", 483 local headformat = "c4BBc6BiBI4BjBn"
484 "\27Lua", -- signature 484 local header = { -- header components
485 0x55, -- version 5.5 (0x55) 485 "\27Lua", -- signature
486 0, -- format 486 0x55, -- version 5.5 (0x55)
487 "\x19\x93\r\n\x1a\n", -- data 487 0, -- format
488 4, -- size of instruction 488 "\x19\x93\r\n\x1a\n", -- a binary string
489 string.packsize("j"), -- sizeof(lua integer) 489 string.packsize("i"), -- size of an int
490 string.packsize("n") -- sizeof(lua number) 490 -0x5678, -- an int
491 ) 491 4, -- size of an instruction
492 0x12345678, -- an instruction (4 bytes)
493 string.packsize("j"), -- size of a Lua integer
494 -0x5678, -- a Lua integer
495 string.packsize("n"), -- size of a Lua float
496 -370.5, -- a Lua float
497 }
498
492 local c = string.dump(function () 499 local c = string.dump(function ()
493 local a = 1; local b = 3; 500 local a = 1; local b = 3;
494 local f = function () return a + b + _ENV.c; end -- upvalues 501 local f = function () return a + b + _ENV.c; end -- upvalues
@@ -500,17 +507,23 @@ do
500 assert(assert(load(c))() == 10) 507 assert(assert(load(c))() == 10)
501 508
502 -- check header 509 -- check header
503 assert(string.sub(c, 1, #header) == header) 510 local t = {string.unpack(headformat, c)}
504 -- check LUAC_INT and LUAC_NUM
505 local ci, cn = string.unpack("jn", c, #header + 1)
506 assert(ci == 0x5678 and cn == 370.5)
507
508 -- corrupted header
509 for i = 1, #header do 511 for i = 1, #header do
512 assert(t[i] == header[i])
513 end
514
515 -- Testing corrupted header.
516 -- A single wrong byte in the head invalidates the chunk,
517 -- except for the Lua float check. (If numbers are long double,
518 -- the representation may need padding, and changing that padding
519 -- will not invalidate the chunk.)
520 local headlen = string.packsize(headformat)
521 headlen = headlen - string.packsize("n") -- remove float check
522 for i = 1, headlen do
510 local s = string.sub(c, 1, i - 1) .. 523 local s = string.sub(c, 1, i - 1) ..
511 string.char(string.byte(string.sub(c, i, i)) + 1) .. 524 string.char((string.byte(string.sub(c, i, i)) + 1) & 0xFF) ..
512 string.sub(c, i + 1, -1) 525 string.sub(c, i + 1, -1)
513 assert(#s == #c) 526 assert(#s == #c and s ~= c)
514 assert(not load(s)) 527 assert(not load(s))
515 end 528 end
516 529