Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix segfault in luv_check_handle/luv_check_stream #634

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 uesrdata to uv_handle_t
Bilal2453 marked this conversation as resolved.
Show resolved Hide resolved
handle = *(uv_handle_t**)udata;
if (!handle || !handle->data) goto fail;
Bilal2453 marked this conversation as resolved.
Show resolved Hide resolved
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 || !handle->data) goto fail;
Bilal2453 marked this conversation as resolved.
Show resolved Hide resolved
return handle;

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