Skip to content

Commit 3b44069

Browse files
committed
Use try/finally when cleaning up multiple resources in case one cleanup fails and throws an exception, the second resource cleanup will still execute.
1 parent 2563596 commit 3b44069

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

src/main/java/com/jcraft/jsch/PageantConnector.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,15 @@ public void query(Buffer buffer) throws AgentProxyException {
136136
"SendMessage() returned 0 with cds.dwData: " + Long.toHexString(foo));
137137
}
138138
} finally {
139-
if (sharedMemory != null)
140-
kernel32.UnmapViewOfFile(sharedMemory);
141-
if (sharedFile != null)
142-
kernel32.CloseHandle(sharedFile);
139+
try {
140+
if (sharedMemory != null) {
141+
kernel32.UnmapViewOfFile(sharedMemory);
142+
}
143+
} finally {
144+
if (sharedFile != null) {
145+
kernel32.CloseHandle(sharedFile);
146+
}
147+
}
143148
}
144149
}
145150

src/main/java23/com/jcraft/jsch/PageantFFMConnector.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,15 @@ public void query(Buffer buffer) throws AgentProxyException {
203203
"SendMessage() returned 0 with cds.dwData: " + Long.toHexString(foo));
204204
}
205205
} finally {
206-
if (!sharedMemory.equals(MemorySegment.NULL))
207-
UnmapViewOfFile(errorState, mmva);
208-
if (!sharedFile.equals(MemorySegment.NULL))
209-
CloseHandle(errorState, sharedFile);
206+
try {
207+
if (!sharedMemory.equals(MemorySegment.NULL)) {
208+
UnmapViewOfFile(errorState, mmva);
209+
}
210+
} finally {
211+
if (!sharedFile.equals(MemorySegment.NULL)) {
212+
CloseHandle(errorState, sharedFile);
213+
}
214+
}
210215
}
211216
}
212217
}
@@ -271,11 +276,14 @@ private MemorySegment getUserSid(Arena arena, MemorySegment errorState)
271276

272277
return usersid;
273278
} finally {
274-
if (!tok.equals(MemorySegment.NULL)) {
275-
CloseHandle(errorState, tok);
276-
}
277-
if (!proc.equals(MemorySegment.NULL)) {
278-
CloseHandle(errorState, proc);
279+
try {
280+
if (!tok.equals(MemorySegment.NULL)) {
281+
CloseHandle(errorState, tok);
282+
}
283+
} finally {
284+
if (!proc.equals(MemorySegment.NULL)) {
285+
CloseHandle(errorState, proc);
286+
}
279287
}
280288
}
281289
}

0 commit comments

Comments
 (0)