Skip to content

Commit

Permalink
fix segfault in luv_check_handle/luv_check_stream (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal2453 authored Mar 3, 2023
1 parent e8e7b7e commit e5da641
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ static uv_handle_t* luv_check_handle(lua_State* L, int index) {
int isHandle;
uv_handle_t* handle;
void *udata;
if (!(udata = lua_touserdata(L, index))) { goto fail; }
if (!(handle = *(uv_handle_t**) udata)) { goto fail; }
if (!handle->data) { goto fail; }
// check if input is a userdata
udata = lua_touserdata(L, index);
if (!udata) goto fail;
// "uv_handle" in the registry is a table structured like so:
// {
// [<uv_aync metatable>] = true,
Expand All @@ -45,11 +45,17 @@ static uv_handle_t* luv_check_handle(lua_State* L, int index) {
// we get its metatable and check that we get `true` back
// when looking the metatable up in the "uv_handle" table.
lua_getfield(L, LUA_REGISTRYINDEX, "uv_handle");
lua_getmetatable(L, index < 0 ? index - 1 : index);
isHandle = lua_getmetatable(L, index < 0 ? index - 1 : index);
if (!isHandle) goto fail;
lua_rawget(L, -2);
isHandle = lua_toboolean(L, -1);
lua_pop(L, 2);
if (isHandle) { return handle; }
if (!isHandle) goto fail;
// cast the userdata to uv_handle_t
handle = *(uv_handle_t**)udata;
if (!handle->data) goto fail;
return handle;

fail: luaL_argerror(L, index, "Expected uv_handle userdata");
return NULL;
}
Expand Down
16 changes: 11 additions & 5 deletions src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ static uv_stream_t* luv_check_stream(lua_State* L, int index) {
int isStream;
void *udata;
uv_stream_t* handle;
if (!(udata = lua_touserdata(L, index))) { goto fail; }
if (!(handle = *(uv_stream_t**) udata)) { goto fail; }
if (!handle->data) { goto fail; }
// check if the input is a userdata
udata = lua_touserdata(L, index);
if (!udata) goto fail;
// "uv_stream" in the registry is a table structured like so:
// {
// [<uv_pipe metatable>] = true,
Expand All @@ -33,11 +33,17 @@ static uv_stream_t* luv_check_stream(lua_State* L, int index) {
// we get its metatable and check that we get `true` back
// when looking the metatable up in the "uv_stream" table.
lua_getfield(L, LUA_REGISTRYINDEX, "uv_stream");
lua_getmetatable(L, index < 0 ? index - 1 : index);
isStream = lua_getmetatable(L, index < 0 ? index - 1 : index);
if (!isStream) goto fail;
lua_rawget(L, -2);
isStream = lua_toboolean(L, -1);
lua_pop(L, 2);
if (isStream) { return handle; }
if (!isStream) goto fail;
// cast the userdata to uv_stream_t
handle = *(uv_stream_t**)udata;
if (!handle->data) goto fail;
return handle;

fail: luaL_argerror(L, index, "Expected uv_stream userdata");
return NULL;
}
Expand Down

0 comments on commit e5da641

Please sign in to comment.