diff --git a/ldump.c b/ldump.c index aca6245b..c4475576 100644 --- a/ldump.c +++ b/ldump.c @@ -198,11 +198,9 @@ static void DumpFunction (const Proto *f, TString *psource, DumpState *D) { static void DumpHeader (DumpState *D) { DumpLiteral(LUA_SIGNATURE, D); - DumpByte(LUAC_VERSION, D); + DumpInt(LUAC_VERSION, D); DumpByte(LUAC_FORMAT, D); DumpLiteral(LUAC_DATA, D); - DumpByte(sizeof(int), D); - DumpByte(sizeof(size_t), D); DumpByte(sizeof(Instruction), D); DumpByte(sizeof(lua_Integer), D); DumpByte(sizeof(lua_Number), D); diff --git a/lundump.c b/lundump.c index 00ff6def..a6004933 100644 --- a/lundump.c +++ b/lundump.c @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include "lua.h" @@ -37,7 +38,7 @@ typedef struct { static l_noret error (LoadState *S, const char *why) { - luaO_pushfstring(S->L, "%s: %s precompiled chunk", S->name, why); + luaO_pushfstring(S->L, "%s: bad binary format (%s)", S->name, why); luaD_throw(S->L, LUA_ERRSYNTAX); } @@ -50,7 +51,7 @@ static l_noret error (LoadState *S, const char *why) { static void LoadBlock (LoadState *S, void *b, size_t size) { if (luaZ_read(S->Z, b, size) != 0) - error(S, "truncated"); + error(S, "truncated chunk"); } @@ -60,24 +61,32 @@ static void LoadBlock (LoadState *S, void *b, size_t size) { static lu_byte LoadByte (LoadState *S) { int b = zgetc(S->Z); if (b == EOZ) - error(S, "truncated"); + error(S, "truncated chunk"); return cast_byte(b); } -static size_t LoadSize (LoadState *S) { +static size_t LoadUnsigned (LoadState *S, size_t limit) { size_t x = 0; int b; + limit >>= 7; do { b = LoadByte(S); + if (x >= limit) + error(S, "integer overflow"); x = (x << 7) | (b & 0x7f); } while ((b & 0x80) == 0); return x; } +static size_t LoadSize (LoadState *S) { + return LoadUnsigned(S, ~(size_t)0); +} + + static int LoadInt (LoadState *S) { - return cast_int(LoadSize(S)); + return cast_int(LoadUnsigned(S, INT_MAX)); } @@ -255,28 +264,27 @@ static void checkliteral (LoadState *S, const char *s, const char *msg) { static void fchecksize (LoadState *S, size_t size, const char *tname) { if (LoadByte(S) != size) - error(S, luaO_pushfstring(S->L, "%s size mismatch in", tname)); + error(S, luaO_pushfstring(S->L, "%s size mismatch", tname)); } #define checksize(S,t) fchecksize(S,sizeof(t),#t) static void checkHeader (LoadState *S) { - checkliteral(S, LUA_SIGNATURE + 1, "not a"); /* 1st char already checked */ - if (LoadByte(S) != LUAC_VERSION) - error(S, "version mismatch in"); + /* 1st char already checked */ + checkliteral(S, LUA_SIGNATURE + 1, "not a binary chunk"); + if (LoadInt(S) != LUAC_VERSION) + error(S, "version mismatch"); if (LoadByte(S) != LUAC_FORMAT) - error(S, "format mismatch in"); - checkliteral(S, LUAC_DATA, "corrupted"); - checksize(S, int); - checksize(S, size_t); + error(S, "format mismatch"); + checkliteral(S, LUAC_DATA, "corrupted chunk"); checksize(S, Instruction); checksize(S, lua_Integer); checksize(S, lua_Number); if (LoadInteger(S) != LUAC_INT) - error(S, "endianness mismatch in"); + error(S, "integer format mismatch"); if (LoadNumber(S) != LUAC_NUM) - error(S, "float format mismatch in"); + error(S, "float format mismatch"); } diff --git a/lundump.h b/lundump.h index 739c7fcd..5b05fed4 100644 --- a/lundump.h +++ b/lundump.h @@ -18,8 +18,7 @@ #define LUAC_INT 0x5678 #define LUAC_NUM cast_num(370.5) -#define MYINT(s) (s[0]-'0') -#define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) +#define LUAC_VERSION LUA_VERSION_NUM #define LUAC_FORMAT 0 /* this is the official format */ /* load one chunk; from lundump.c */ diff --git a/testes/calls.lua b/testes/calls.lua index f5108a47..941493b1 100644 --- a/testes/calls.lua +++ b/testes/calls.lua @@ -397,17 +397,15 @@ assert((function (a) return a end)() == nil) print("testing binary chunks") do - local header = string.pack("c4BBc6BBBBBj", - "\27Lua", -- signature - 5*16 + 4, -- version 5.4 - 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 + local header = string.pack("c4BBBc6BBBj", + "\27Lua", -- signature + (504 >> 7) & 0x7f, (504 & 0x7f) | 0x80, -- version 5.4 (504) + 0, -- format + "\x19\x93\r\n\x1a\n", -- data + 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)