src: refactor win32 DebugProcess() to use RAII cleanup

Prefer more idiomatic C++ cleanup code over `goto`.

PR-URL: https://github.com/nodejs/node/pull/22981
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
This commit is contained in:
Anna Henningsen authored and Michaël Zasso committed 2018-09-24 21:40:50 +02:00
1 parent 5d4bec3e0f
commit ab032e4ff4
1 file changed
+22 -20
+22 -20
View File
@@ -2273,6 +2273,12 @@ static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
static void DebugProcess(const FunctionCallbackInfo<Value>& 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<Value>& 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<Integer>()->Value();
@@ -2296,14 +2308,14 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& 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<Value>& args) {
isolate->ThrowException(WinapiErrnoException(isolate,
GetLastError(),
"OpenFileMappingW"));
goto out;
return;
}
handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
@@ -2323,7 +2335,7 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& 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<Value>& 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<Value>& 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