From 3ba0fe6fd5ad9f62b948bf970347ffef3d5fe4bf Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 8 May 2017 13:34:32 +0200 Subject: Fix bad integer size for string.pack/packsize --- README.md | 8 ++-- src/lstrlib.js | 4 +- tests/lstrlib.js | 2 +- tests/test-suite/calls.js | 119 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 40ca22c..fae35c3 100644 --- a/README.md +++ b/README.md @@ -66,9 +66,10 @@ - [ ] `luaL_ref` - [ ] `luaL_unref` - [ ] Run [Lua test suite](https://github.com/lua/tests) - - [x] `constructs.lua (`_soft`) - - [x] `locals.lua - - [x] `strings.lua + - [x] `calls.lua` + - [x] `constructs.lua` (`_soft`) + - [x] `locals.lua` + - [x] `strings.lua` - [ ] `all.lua` - [ ] `big.lua` - [ ] `checktable.lua` @@ -87,7 +88,6 @@ - [ ] `nextvar.lua` - [ ] `vararg.lua` - [ ] `attrib.lua` - - [ ] `calls.lua` - [ ] `code.lua` - [ ] `db.lua` - [ ] `files.lua` diff --git a/src/lstrlib.js b/src/lstrlib.js index f5b9f34..2eb9ff5 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -368,7 +368,7 @@ const LUAL_PACKPADBYTE = 0x00; /* maximum size for the binary representation of an integer */ const MAXINTSIZE = 16; -const SZINT = 8; // Size of lua_Integer +const SZINT = 4; // Size of lua_Integer /* number of bits in a character */ const NB = 8; @@ -449,7 +449,7 @@ const getoption = function(h, fmt) { case 'H'.charCodeAt(0): r.size = 2; r.opt = KOption.Kuint; return r; case 'l'.charCodeAt(0): r.size = 8; r.opt = KOption.Kint; return r; // sizeof(long): 8 case 'L'.charCodeAt(0): r.size = 8; r.opt = KOption.Kuint; return r; - case 'j'.charCodeAt(0): r.size = 8; r.opt = KOption.Kint; return r; // sizeof(lua_Integer): 8 + case 'j'.charCodeAt(0): r.size = 4; r.opt = KOption.Kint; return r; // sizeof(lua_Integer): 8 case 'J'.charCodeAt(0): r.size = 8; r.opt = KOption.Kuint; return r; case 'T'.charCodeAt(0): r.size = 8; r.opt = KOption.Kuint; return r; // sizeof(size_t): 8 case 'f'.charCodeAt(0): r.size = 4; r.opt = KOption.Kfloat; return r; // sizeof(float): 4 diff --git a/tests/lstrlib.js b/tests/lstrlib.js index b9d630d..1012f65 100644 --- a/tests/lstrlib.js +++ b/tests/lstrlib.js @@ -485,7 +485,7 @@ test('string.pack/unpack/packsize', function (t) { t.strictEqual( lua.lua_tointeger(L, -2), - 16, + 12, "Correct element(s) on the stack" ); diff --git a/tests/test-suite/calls.js b/tests/test-suite/calls.js index 6396dd8..15e9998 100644 --- a/tests/test-suite/calls.js +++ b/tests/test-suite/calls.js @@ -634,3 +634,122 @@ test("[test-suite] calls: test for dump/undump with many upvalues", function (t) }, "Lua program ran without error"); }); + + +test("[test-suite] calls: test for long method names", function (t) { + let luaCode = ` + do + local t = {x = 1} + function t:_012345678901234567890123456789012345678901234567890123456789 () + return self.x + end + assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1) + end + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: test for bug in parameter adjustment", function (t) { + let luaCode = ` + assert((function () return nil end)(4) == nil) + assert((function () local a; return a end)(4) == nil) + assert((function (a) return a end)() == nil) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("[test-suite] calls: testing binary chunks", function (t) { + let luaCode = ` + do + local header = string.pack("c4BBc6BBBBBj", + "\\27Lua", -- signature + 5*16 + 3, -- version 5.3 + 0, -- format + "\\x19\\x93\\r\\n\\x1a\\n", -- data + string.packsize("i"), -- sizeof(int) + string.packsize("T"), -- sizeof(size_t) + 4, -- size of instruction + string.packsize("j"), -- sizeof(lua integer) + string.packsize("n"), -- sizeof(lua number) + 0x5678 -- LUAC_INT + -- LUAC_NUM may not have a unique binary representation (padding...) + ) + local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end) + + assert(string.sub(c, 1, #header) == header) + + -- corrupted header + for i = 1, #header do + local s = string.sub(c, 1, i - 1) .. + string.char(string.byte(string.sub(c, i, i)) + 1) .. + string.sub(c, i + 1, -1) + assert(#s == #c) + assert(not load(s)) + end + + -- loading truncated binary chunks + for i = 1, #c - 1 do + local st, msg = load(string.sub(c, 1, i)) + assert(not st and string.find(msg, "truncated")) + end + assert(assert(load(c))() == 10) + end + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + lauxlib.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lua.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); -- cgit v1.2.3-70-g09d2