From eb4d7f57d5f98cf05f160d74b014f5bdc7a529a8 Mon Sep 17 00:00:00 2001 From: legendecas Date: Fri, 11 Jun 2021 00:54:58 +0800 Subject: [PATCH] src: cleanup uv_fs_t regardless of success or not PR-URL: https://github.com/nodejs/node/pull/38996 Reviewed-By: Joyee Cheung Reviewed-By: Franziska Hinkelmann --- src/util.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/util.cc b/src/util.cc index 40ea25b8d2edfe..e181fdf568cd18 100644 --- a/src/util.cc +++ b/src/util.cc @@ -223,8 +223,13 @@ int WriteFileSync(v8::Isolate* isolate, int ReadFileSync(std::string* result, const char* path) { uv_fs_t req; + auto defer_req_cleanup = OnScopeLeave([&req]() { + uv_fs_req_cleanup(&req); + }); + uv_file file = uv_fs_open(nullptr, &req, path, O_RDONLY, 0, nullptr); if (req.result < 0) { + // req will be cleaned up by scope leave. return req.result; } uv_fs_req_cleanup(&req); @@ -243,7 +248,7 @@ int ReadFileSync(std::string* result, const char* path) { const int r = uv_fs_read(nullptr, &req, file, &buf, 1, result->length(), nullptr); if (req.result < 0) { - uv_fs_req_cleanup(&req); + // req will be cleaned up by scope leave. return req.result; } uv_fs_req_cleanup(&req);