From 03b65225d234b9d2c225f307d1ee1d0ff39d8123 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 5 Dec 2019 10:12:22 -0500 Subject: [PATCH 1/2] src: use checked allocations in WASI::New() --- src/node_wasi.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/node_wasi.cc b/src/node_wasi.cc index 8de7f1fc1cae68..b3b266f5cfe577 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -134,7 +134,7 @@ void WASI::New(const FunctionCallbackInfo& args) { Local preopens = args[2].As(); CHECK_EQ(preopens->Length() % 2, 0); options.preopenc = preopens->Length() / 2; - options.preopens = UncheckedCalloc(options.preopenc); + options.preopens = Calloc(options.preopenc); int index = 0; for (uint32_t i = 0; i < preopens->Length(); i += 2) { auto mapped = preopens->Get(context, i).ToLocalChecked(); @@ -144,7 +144,9 @@ void WASI::New(const FunctionCallbackInfo& args) { node::Utf8Value mapped_path(env->isolate(), mapped); node::Utf8Value real_path(env->isolate(), real); options.preopens[index].mapped_path = strdup(*mapped_path); + CHECK_NOT_NULL(options.preopens[index].mapped_path); options.preopens[index].real_path = strdup(*real_path); + CHECK_NOT_NULL(options.preopens[index].real_path); index++; } From 40c4ec3968db2422a5c0c3e81450b3ea21132681 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 5 Dec 2019 10:21:16 -0500 Subject: [PATCH 2/2] src: free preopen memory in WASI::New() --- src/node_wasi.cc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/node_wasi.cc b/src/node_wasi.cc index b3b266f5cfe577..e9bcb42ad6b0d9 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -163,6 +163,15 @@ void WASI::New(const FunctionCallbackInfo& args) { free(options.envp[i]); delete[] options.envp; } + + if (options.preopens != nullptr) { + for (uint32_t i = 0; i < options.preopenc; i++) { + free(options.preopens[i].mapped_path); + free(options.preopens[i].real_path); + } + + delete[] options.preopens; + } }