From ab032e4ff4f0e295402964d077a28e40d26e3734 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Thu, 20 Sep 2018 21:16:28 +0200 Subject: [PATCH] src: refactor win32 `DebugProcess()` to use RAII cleanup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prefer more idiomatic C++ cleanup code over `goto`. PR-URL: https://github.com/nodejs/node/pull/22981 Reviewed-By: Tobias Nießen Reviewed-By: Eugene Ostroukhov Reviewed-By: Bartosz Sosnowski Reviewed-By: Denys Otrishko Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Gireesh Punathil --- src/node.cc | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/node.cc b/src/node.cc index 1f23da8c5eed38..678c1154b0357b 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2273,6 +2273,12 @@ static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf, static void DebugProcess(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = args.GetIsolate(); + + if (args.Length() != 1) { + env->ThrowError("Invalid number of arguments."); + return; + } + HANDLE process = nullptr; HANDLE thread = nullptr; HANDLE mapping = nullptr; @@ -2280,10 +2286,16 @@ static void DebugProcess(const FunctionCallbackInfo& args) { LPTHREAD_START_ROUTINE* handler = nullptr; DWORD pid = 0; - if (args.Length() != 1) { - env->ThrowError("Invalid number of arguments."); - goto out; - } + OnScopeLeave cleanup([&]() { + if (process != nullptr) + CloseHandle(process); + if (thread != nullptr) + CloseHandle(thread); + if (handler != nullptr) + UnmapViewOfFile(handler); + if (mapping != nullptr) + CloseHandle(mapping); + }); CHECK(args[0]->IsNumber()); pid = args[0].As()->Value(); @@ -2296,14 +2308,14 @@ static void DebugProcess(const FunctionCallbackInfo& args) { if (process == nullptr) { isolate->ThrowException( WinapiErrnoException(isolate, GetLastError(), "OpenProcess")); - goto out; + return; } if (GetDebugSignalHandlerMappingName(pid, mapping_name, arraysize(mapping_name)) < 0) { env->ThrowErrnoException(errno, "sprintf"); - goto out; + return; } mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name); @@ -2311,7 +2323,7 @@ static void DebugProcess(const FunctionCallbackInfo& args) { isolate->ThrowException(WinapiErrnoException(isolate, GetLastError(), "OpenFileMappingW")); - goto out; + return; } handler = reinterpret_cast( @@ -2323,7 +2335,7 @@ static void DebugProcess(const FunctionCallbackInfo& args) { if (handler == nullptr || *handler == nullptr) { isolate->ThrowException( WinapiErrnoException(isolate, GetLastError(), "MapViewOfFile")); - goto out; + return; } thread = CreateRemoteThread(process, @@ -2337,7 +2349,7 @@ static void DebugProcess(const FunctionCallbackInfo& args) { isolate->ThrowException(WinapiErrnoException(isolate, GetLastError(), "CreateRemoteThread")); - goto out; + return; } // Wait for the thread to terminate @@ -2345,18 +2357,8 @@ static void DebugProcess(const FunctionCallbackInfo& args) { isolate->ThrowException(WinapiErrnoException(isolate, GetLastError(), "WaitForSingleObject")); - goto out; - } - - out: - if (process != nullptr) - CloseHandle(process); - if (thread != nullptr) - CloseHandle(thread); - if (handler != nullptr) - UnmapViewOfFile(handler); - if (mapping != nullptr) - CloseHandle(mapping); + return; + } } #endif // _WIN32