ping: place IcmpCloseHandle in finally

If toIntExact() throws then this would leak the handle
This commit is contained in:
Adam
2020-12-20 12:54:22 -05:00
parent ab4cf1afe9
commit 175f436f48

View File

@@ -66,6 +66,8 @@ public class Ping
{ {
IPHlpAPI ipHlpAPI = IPHlpAPI.INSTANCE; IPHlpAPI ipHlpAPI = IPHlpAPI.INSTANCE;
Pointer ptr = ipHlpAPI.IcmpCreateFile(); Pointer ptr = ipHlpAPI.IcmpCreateFile();
try
{
InetAddress inetAddress = InetAddress.getByName(world.getAddress()); InetAddress inetAddress = InetAddress.getByName(world.getAddress());
byte[] address = inetAddress.getAddress(); byte[] address = inetAddress.getAddress();
String dataStr = RUNELITE_PING; String dataStr = RUNELITE_PING;
@@ -78,14 +80,15 @@ public class Ping
int ret = ipHlpAPI.IcmpSendEcho(ptr, packed, data, (short) (dataLength), Pointer.NULL, icmpEchoReply, IcmpEchoReply.SIZE + dataLength, TIMEOUT); int ret = ipHlpAPI.IcmpSendEcho(ptr, packed, data, (short) (dataLength), Pointer.NULL, icmpEchoReply, IcmpEchoReply.SIZE + dataLength, TIMEOUT);
if (ret != 1) if (ret != 1)
{ {
ipHlpAPI.IcmpCloseHandle(ptr);
return -1; return -1;
} }
int rtt = Math.toIntExact(icmpEchoReply.roundTripTime.longValue()); return Math.toIntExact(icmpEchoReply.roundTripTime.longValue());
}
finally
{
ipHlpAPI.IcmpCloseHandle(ptr); ipHlpAPI.IcmpCloseHandle(ptr);
}
return rtt;
} }
private static int tcpPing(World world) throws IOException private static int tcpPing(World world) throws IOException