Skip to content

Commit 59a8324

Browse files
committed
src: refactor win32 DebugProcess() to use RAII cleanup
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>
1 parent bea41bc commit 59a8324

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
@@ -2266,17 +2266,29 @@ static int GetDebugSignalHandlerMappingName(DWORD pid, wchar_t* buf,
22662266
static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
22672267
Environment* env = Environment::GetCurrent(args);
22682268
Isolate* isolate = args.GetIsolate();
2269+
2270+
if (args.Length() != 1) {
2271+
env->ThrowError("Invalid number of arguments.");
2272+
return;
2273+
}
2274+
22692275
HANDLE process = nullptr;
22702276
HANDLE thread = nullptr;
22712277
HANDLE mapping = nullptr;
22722278
wchar_t mapping_name[32];
22732279
LPTHREAD_START_ROUTINE* handler = nullptr;
22742280
DWORD pid = 0;
22752281

2276-
if (args.Length() != 1) {
2277-
env->ThrowError("Invalid number of arguments.");
2278-
goto out;
2279-
}
2282+
OnScopeLeave cleanup([&]() {
2283+
if (process != nullptr)
2284+
CloseHandle(process);
2285+
if (thread != nullptr)
2286+
CloseHandle(thread);
2287+
if (handler != nullptr)
2288+
UnmapViewOfFile(handler);
2289+
if (mapping != nullptr)
2290+
CloseHandle(mapping);
2291+
});
22802292

22812293
CHECK(args[0]->IsNumber());
22822294
pid = args[0].As<Integer>()->Value();
@@ -2289,22 +2301,22 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
22892301
if (process == nullptr) {
22902302
isolate->ThrowException(
22912303
WinapiErrnoException(isolate, GetLastError(), "OpenProcess"));
2292-
goto out;
2304+
return;
22932305
}
22942306

22952307
if (GetDebugSignalHandlerMappingName(pid,
22962308
mapping_name,
22972309
arraysize(mapping_name)) < 0) {
22982310
env->ThrowErrnoException(errno, "sprintf");
2299-
goto out;
2311+
return;
23002312
}
23012313

23022314
mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name);
23032315
if (mapping == nullptr) {
23042316
isolate->ThrowException(WinapiErrnoException(isolate,
23052317
GetLastError(),
23062318
"OpenFileMappingW"));
2307-
goto out;
2319+
return;
23082320
}
23092321

23102322
handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>(
@@ -2316,7 +2328,7 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
23162328
if (handler == nullptr || *handler == nullptr) {
23172329
isolate->ThrowException(
23182330
WinapiErrnoException(isolate, GetLastError(), "MapViewOfFile"));
2319-
goto out;
2331+
return;
23202332
}
23212333

23222334
thread = CreateRemoteThread(process,
@@ -2330,26 +2342,16 @@ static void DebugProcess(const FunctionCallbackInfo<Value>& args) {
23302342
isolate->ThrowException(WinapiErrnoException(isolate,
23312343
GetLastError(),
23322344
"CreateRemoteThread"));
2333-
goto out;
2345+
return;
23342346
}
23352347

23362348
// Wait for the thread to terminate
23372349
if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) {
23382350
isolate->ThrowException(WinapiErrnoException(isolate,
23392351
GetLastError(),
23402352
"WaitForSingleObject"));
2341-
goto out;
2342-
}
2343-
2344-
out:
2345-
if (process != nullptr)
2346-
CloseHandle(process);
2347-
if (thread != nullptr)
2348-
CloseHandle(thread);
2349-
if (handler != nullptr)
2350-
UnmapViewOfFile(handler);
2351-
if (mapping != nullptr)
2352-
CloseHandle(mapping);
2353+
return;
2354+
}
23532355
}
23542356
#endif // _WIN32
23552357

0 commit comments

Comments
 (0)