lua-users home
lua-l archive

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


It was thus said that the Great Tim Hill once stated:
> In lua_pushlstring():
> 
> 	const char *lua_pushlstring (lua_State *L, const char *s, size_t len);
> 
> The docs don’t appear to specify what is allowable input when “len” is
> zero (that is, pushing an empty string). Can “s” be NULL in this case? We
> have a number of libraries that tend to return empty data as NULL pointer
> plus zero length. To be safe I coerce “s” to point to an arbitrary (valid)
> memory location, but it would be nice to just pass through the NULL in
> this case and save the extra logic.

  I traced the code in Lua 5.3 to see what happens.  It will end up in
internshrstr() (lstring.c:122) where the pointer is passed (along with the
length), without checking if it's NULL, to memcmp().  The C standard also
has very little to say about passing a NULL pointer with a length of 0 to
memcmp().

  Now, I did do a test:

	#include <stdlib.h>
	#include <lua.h>
	#include <lauxlib.h>

	int main(void)
	{
	  const char *c;
	  
	  lua_State *L = luaL_newstate();
	  lua_pushlstring(L,NULL,0);
	  c = lua_tostring(L,-1);
	  printf("[%s]\n",c);
	  lua_close(L);
	  return EXIT_SUCCESS;
	}

and compiled it both for Lua 5.1 and 5.3, and it did "Do The Right Thing."
But that was on my system---your milage may vary.

  -spc (Caveat emptor)