Skip to content

Commit

Permalink
deps: uvwasi: cherry-pick 75b389c
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cjihrig authored and targos committed Jan 14, 2020
1 parent a9d9fdb commit 6784296
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions deps/uvwasi/src/fd_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,25 @@ uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
return UVWASI_EINVAL;

table->fds = NULL;
r = uv_rwlock_init(&table->rwlock);
if (r != 0)
return uvwasi__translate_uv_error(r);

table->used = 0;
table->size = init_size;
table->fds = uvwasi__calloc(uvwasi,
init_size,
sizeof(struct uvwasi_fd_wrap_t*));

if (table->fds == NULL) {
err = UVWASI_ENOMEM;
goto error_exit;
if (table->fds == NULL)
return UVWASI_ENOMEM;

r = uv_rwlock_init(&table->rwlock);
if (r != 0) {
err = uvwasi__translate_uv_error(r);
/* Free table->fds and set it to NULL here. This is done explicitly instead
of jumping to error_exit because uvwasi_fd_table_free() relies on fds
being NULL to know whether or not to destroy the rwlock.
*/
uvwasi__free(uvwasi, table->fds);
table->fds = NULL;
return err;
}

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

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

0 comments on commit 6784296

Please sign in to comment.