Skip to content

Commit

Permalink
src: refactor win32 DebugProcess() to use RAII cleanup
Browse files Browse the repository at this point in the history
Prefer more idiomatic C++ cleanup code over `goto`.

PR-URL: #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>
  • Loading branch information
addaleax committed Sep 23, 2018
1 parent bea41bc commit 59a8324
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2266,17 +2266,29 @@ 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;
wchar_t mapping_name[32];
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();
Expand All @@ -2289,22 +2301,22 @@ 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);
if (mapping == nullptr) {
isolate->ThrowException(WinapiErrnoException(isolate,
GetLastError(),
"OpenFileMappingW"));
goto out;
return;
}

handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
Expand All @@ -2316,7 +2328,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,
Expand All @@ -2330,26 +2342,16 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
isolate->ThrowException(WinapiErrnoException(isolate,
GetLastError(),
"CreateRemoteThread"));
goto out;
return;
}

// Wait for the thread to terminate
if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
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

Expand Down

0 comments on commit 59a8324

Please sign in to comment.