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 Sean Conner once stated:
>
> 	static int mydebug_mygetstack(lua_State *L)
> 	{
> 	  lua_Debug *ar;
> 	  
> 	  ar = malloc(sizeof(lua_Debug));
> 	  if (ar != NULL)
> 	  {
> 	    lua_getstack(L,luaL_checkinteger(L,1),ar);
> 	    lua_pushfstring(L,"activation record: %p",(void *)ar);
> 	  }
> 	  else
> 	    lua_pushnil(L);
> 	  
> 	  return 1;
> 	}

  Oops, this can leak memory when it's not given an integer (it's rare that
I call malloc() in Lua interface code).  This should be:

	static int mydebug_mygetstack(lua_State *L)
	{
	  lua_Debug *ar;
	  int        level = luaL_checkinteger(L,1);
	  
	  ar = malloc(sizeof(lua_Debug));
	  if (ar != NULL)
	  {
	    lua_getstack(L,level,&ar);
	    lua_pushfstring(L,"activation record: %p",(void *)ar);
	  }
	  else
	    lua_pushnil(L);
	  
	  return 1;
	}

  -spc (That will avoid the memory leak on a bad parameter)