Re: Adding another way to point to "levels" to debug.getinfo and friends
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Adding another way to point to "levels" to debug.getinfo and friends
- From: Sean Conner <sean@...>
- Date: 2019年5月12日 19:09:53 -0400
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)