Skip to content

Commit 6de77d3

Browse files
cjihrigBethGriggs
authored andcommitted
deps: uvwasi: cherry-pick 75b389c
Original commit message: This commit changes the memory management in uvwasi_fd_table_init() such that ENOMEM errors can be handled better in uvwasi_fd_table_free(). PR-URL: #31076 Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 8f4339b commit 6de77d3

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

‎deps/uvwasi/src/fd_table.c‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,19 +291,25 @@ uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
291291
returnUVWASI_EINVAL;
292292

293293
table->fds=NULL;
294-
r=uv_rwlock_init(&table->rwlock);
295-
if (r!=0)
296-
returnuvwasi__translate_uv_error(r);
297-
298294
table->used=0;
299295
table->size=init_size;
300296
table->fds=uvwasi__calloc(uvwasi,
301297
init_size,
302298
sizeof(structuvwasi_fd_wrap_t*));
303299

304-
if (table->fds==NULL){
305-
err=UVWASI_ENOMEM;
306-
goto error_exit;
300+
if (table->fds==NULL)
301+
returnUVWASI_ENOMEM;
302+
303+
r=uv_rwlock_init(&table->rwlock);
304+
if (r!=0){
305+
err=uvwasi__translate_uv_error(r);
306+
/* Free table->fds and set it to NULL here. This is done explicitly instead
307+
of jumping to error_exit because uvwasi_fd_table_free() relies on fds
308+
being NULL to know whether or not to destroy the rwlock.
309+
*/
310+
uvwasi__free(uvwasi, table->fds);
311+
table->fds=NULL;
312+
returnerr;
307313
}
308314

309315
/* Create the stdio FDs. */
@@ -358,13 +364,6 @@ void uvwasi_fd_table_free(uvwasi_t* uvwasi, struct uvwasi_fd_table_t* table){
358364
}
359365

360366
if (table->fds!=NULL){
361-
/* It's fine to call uvwasi__free() multiple times on table->fds. However,
362-
it is not fine to call uv_rwlock_destroy() multiple times. Guard against
363-
that by ensuring that table->fds is not NULL. Technically, it's possible
364-
that uvwasi_fd_table_init() initialized the rwlock successfully, but
365-
failed to initialize fds. However, the only way that's possible is if
366-
the application already ran out of memory.
367-
*/
368367
uvwasi__free(uvwasi, table->fds);
369368
table->fds=NULL;
370369
table->size=0;

0 commit comments

Comments
(0)