diff --git a/src/node.cc b/src/node.cc index 1f23da8c5ee..678c1154b03 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; + return; } - - out: - if (process != nullptr) - CloseHandle(process); - if (thread != nullptr) - CloseHandle(thread); - if (handler != nullptr) - UnmapViewOfFile(handler); - if (mapping != nullptr) - CloseHandle(mapping); } #endif // _WIN32