Skip to content

Commit af434d1

Browse files
committed
fs: Don't unref luv_dir_t until after we're actually sure it's okay to GC it
It was technically possible for the luv_dir_t to be garbage collected, which would lead to the dirent data being unref'ed and that *also* could garbage collected, before one of the luv_push_dirent calls that use the dirent data (i.e. a possible use-after-free). Seems very unlikely in real-world usage, but 100% reproducible with a maximally aggressive/stop-the-world GC.
1 parent bcb0f40 commit af434d1

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/fs.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,6 @@ static int push_fs_result(lua_State* L, uv_fs_t* req) {
374374
return 1;
375375
}
376376
case UV_FS_READDIR: {
377-
luaL_unref(L, LUA_REGISTRYINDEX, data->data_ref);
378-
data->data_ref = LUA_NOREF;
379-
380377
if(req->result > 0) {
381378
size_t i;
382379
uv_dir_t *dir = (uv_dir_t*)req->ptr;
@@ -388,6 +385,8 @@ static int push_fs_result(lua_State* L, uv_fs_t* req) {
388385
} else
389386
lua_pushnil(L);
390387

388+
luaL_unref(L, LUA_REGISTRYINDEX, data->data_ref);
389+
data->data_ref = LUA_NOREF;
391390
return 1;
392391
}
393392
case UV_FS_CLOSEDIR:

0 commit comments

Comments
 (0)