Skip to content

Commit ab032e4

Browse files
addaleaxtargos
authored andcommitted
src: refactor win32 DebugProcess() to use RAII cleanup
Prefer more idiomatic C++ cleanup code over `goto`. PR-URL: #22981 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Bartosz Sosnowski <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 5d4bec3 commit ab032e4

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/node.cc

+23-21
Original file line numberDiff line numberDiff line change
@@ -2273,17 +2273,29 @@ static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
22732273
static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
22742274
Environment* env = Environment::GetCurrent(args);
22752275
Isolate* isolate = args.GetIsolate();
2276+
2277+
if (args.Length() != 1) {
2278+
env->ThrowError("Invalid number of arguments.");
2279+
return;
2280+
}
2281+
22762282
HANDLE process = nullptr;
22772283
HANDLE thread = nullptr;
22782284
HANDLE mapping = nullptr;
22792285
wchar_t mapping_name[32];
22802286
LPTHREAD_START_ROUTINE* handler = nullptr;
22812287
DWORD pid = 0;
22822288

2283-
if (args.Length() != 1) {
2284-
env->ThrowError("Invalid number of arguments.");
2285-
goto out;
2286-
}
2289+
OnScopeLeave cleanup([&]() {
2290+
if (process != nullptr)
2291+
CloseHandle(process);
2292+
if (thread != nullptr)
2293+
CloseHandle(thread);
2294+
if (handler != nullptr)
2295+
UnmapViewOfFile(handler);
2296+
if (mapping != nullptr)
2297+
CloseHandle(mapping);
2298+
});
22872299

22882300
CHECK(args[0]->IsNumber());
22892301
pid = args[0].As<Integer>()->Value();
@@ -2296,22 +2308,22 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
22962308
if (process == nullptr) {
22972309
isolate->ThrowException(
22982310
WinapiErrnoException(isolate, GetLastError(), "OpenProcess"));
2299-
goto out;
2311+
return;
23002312
}
23012313

23022314
if (GetDebugSignalHandlerMappingName(pid,
23032315
mapping_name,
23042316
arraysize(mapping_name)) < 0) {
23052317
env->ThrowErrnoException(errno, "sprintf");
2306-
goto out;
2318+
return;
23072319
}
23082320

23092321
mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
23102322
if (mapping == nullptr) {
23112323
isolate->ThrowException(WinapiErrnoException(isolate,
23122324
GetLastError(),
23132325
"OpenFileMappingW"));
2314-
goto out;
2326+
return;
23152327
}
23162328

23172329
handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
@@ -2323,7 +2335,7 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
23232335
if (handler == nullptr || *handler == nullptr) {
23242336
isolate->ThrowException(
23252337
WinapiErrnoException(isolate, GetLastError(), "MapViewOfFile"));
2326-
goto out;
2338+
return;
23272339
}
23282340

23292341
thread = CreateRemoteThread(process,
@@ -2337,26 +2349,16 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
23372349
isolate->ThrowException(WinapiErrnoException(isolate,
23382350
GetLastError(),
23392351
"CreateRemoteThread"));
2340-
goto out;
2352+
return;
23412353
}
23422354

23432355
// Wait for the thread to terminate
23442356
if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
23452357
isolate->ThrowException(WinapiErrnoException(isolate,
23462358
GetLastError(),
23472359
"WaitForSingleObject"));
2348-
goto out;
2349-
}
2350-
2351-
out:
2352-
if (process != nullptr)
2353-
CloseHandle(process);
2354-
if (thread != nullptr)
2355-
CloseHandle(thread);
2356-
if (handler != nullptr)
2357-
UnmapViewOfFile(handler);
2358-
if (mapping != nullptr)
2359-
CloseHandle(mapping);
2360+
return;
2361+
}
23602362
}
23612363
#endif // _WIN32
23622364

0 commit comments

Comments
 (0)