lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Here's what appears to be an inconsistency in the Lua sources.

This comes from lua.h:
	/* mark for precompiled code (`<esc>Lua') */
	#define	LUA_SIGNATURE	"\033Lua"

In lundump.h, there's:
	/* size of header of binary files */
	#define LUAC_HEADERSIZE		 12

And finally, from lundump.c:
	static void LoadHeader(LoadState* S)
	{
	 char h[LUAC_HEADERSIZE];
	 char s[LUAC_HEADERSIZE];
	 luaU_header(h);
	 LoadBlock(S,s,LUAC_HEADERSIZE);
	 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
	}

... as well as:
	void luaU_header (char* h)
	{
	 int x=1;
	 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
	 h+=sizeof(LUA_SIGNATURE)-1;
	 *h++=(char)LUAC_VERSION;
	 *h++=(char)LUAC_FORMAT;
	 *h++=(char)*(char*)&x;		 	 	 /* endianness */
	 *h++=(char)sizeof(int);
	 *h++=(char)sizeof(size_t);
	 *h++=(char)sizeof(Instruction);
	 *h++=(char)sizeof(lua_Number);
	 *h++=(char)(((lua_Number)0.5)==0);		 /* is lua_Number integral? */
	}

What happens if someone changes LUA_SIGNATURE in lua.h to something longer than
4 characters? Well, I did (after checking all obvious places where the
signature's used) and recompiled. To my surprise, the resulting luac executable
crashed... so I started looking at some less obvious places and found that these
functions in lundump.c simply suppose a signature length of 4 characters or less.

Perhaps there should be a warning in lua.h? Or somewhat more robust code in
lundump.c?