Skip to content

Commit 04fca35

Browse files
committed
added tail data to header
better error message instead of "bad header"
1 parent aa3c1ec commit 04fca35

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lundump.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lundump.c,v 1.66 2009/05/01 01:25:34 lhf Exp lhf $
2+
** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf Exp lhf $
33
** load precompiled Lua chunks
44
** See Copyright Notice in lua.h
55
*/
@@ -175,13 +175,22 @@ static Proto* LoadFunction(LoadState* S)
175175
return f;
176176
}
177177

178+
/* the code below must be consistent with the code in luaU_header */
179+
#define N0 LUAC_HEADERSIZE
180+
#define N1 (sizeof(LUA_SIGNATURE)-1)
181+
#define N2 N1+2
182+
#define N3 N2+6
183+
178184
static void LoadHeader(LoadState* S)
179185
{
180186
char h[LUAC_HEADERSIZE];
181187
char s[LUAC_HEADERSIZE];
182188
luaU_header(h);
183189
LoadBlock(S,s,LUAC_HEADERSIZE);
184-
if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible");
190+
if (memcmp(h,s,N0)==0) return;
191+
if (memcmp(h,s,N1)!=0) error(S,"not a");
192+
if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
193+
if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
185194
}
186195

187196
/*
@@ -203,22 +212,26 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
203212
return LoadFunction(&S);
204213
}
205214

215+
/* data to catch conversion errors */
216+
#define TAIL "\x19\x93\r\n\x1a\n"
217+
206218
/*
207-
* make header
208-
* if you make any changes in the header or in LUA_SIGNATURE,
209-
* be sure to update LUAC_HEADERSIZE accordingly in lundump.h.
219+
* make header for precompiled chunks
220+
* if you make any changes in the code below or in LUA_SIGNATURE in lua.h,
221+
* be sure to update LoadHeader above and LUAC_HEADERSIZE in lundump.h
210222
*/
211223
void luaU_header (char* h)
212224
{
213225
int x=1;
214226
memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
215227
h+=sizeof(LUA_SIGNATURE)-1;
216-
*h++=(char)LUAC_VERSION;
217-
*h++=(char)LUAC_FORMAT;
228+
*h++=(char)0x52; /* Lua 5.2 */
229+
*h++=(char)0; /* the official format */
218230
*h++=(char)*(char*)&x; /* endianness */
219231
*h++=(char)sizeof(int);
220232
*h++=(char)sizeof(size_t);
221233
*h++=(char)sizeof(Instruction);
222234
*h++=(char)sizeof(lua_Number);
223235
*h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
236+
memcpy(h,TAIL,sizeof(TAIL)-1);
224237
}

0 commit comments

Comments
 (0)