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,26 +66,29 @@ public class Ping
{ {
IPHlpAPI ipHlpAPI = IPHlpAPI.INSTANCE; IPHlpAPI ipHlpAPI = IPHlpAPI.INSTANCE;
Pointer ptr = ipHlpAPI.IcmpCreateFile(); Pointer ptr = ipHlpAPI.IcmpCreateFile();
InetAddress inetAddress = InetAddress.getByName(world.getAddress()); try
byte[] address = inetAddress.getAddress(); {
String dataStr = RUNELITE_PING; InetAddress inetAddress = InetAddress.getByName(world.getAddress());
int dataLength = dataStr.length() + 1; byte[] address = inetAddress.getAddress();
Pointer data = new Memory(dataLength); String dataStr = RUNELITE_PING;
data.setString(0L, dataStr); int dataLength = dataStr.length() + 1;
IcmpEchoReply icmpEchoReply = new IcmpEchoReply(new Memory(IcmpEchoReply.SIZE + dataLength)); Pointer data = new Memory(dataLength);
assert icmpEchoReply.size() == IcmpEchoReply.SIZE; data.setString(0L, dataStr);
int packed = (address[0] & 0xff) | ((address[1] & 0xff) << 8) | ((address[2] & 0xff) << 16) | ((address[3] & 0xff) << 24); IcmpEchoReply icmpEchoReply = new IcmpEchoReply(new Memory(IcmpEchoReply.SIZE + dataLength));
int ret = ipHlpAPI.IcmpSendEcho(ptr, packed, data, (short) (dataLength), Pointer.NULL, icmpEchoReply, IcmpEchoReply.SIZE + dataLength, TIMEOUT); assert icmpEchoReply.size() == IcmpEchoReply.SIZE;
if (ret != 1) int packed = (address[0] & 0xff) | ((address[1] & 0xff) << 8) | ((address[2] & 0xff) << 16) | ((address[3] & 0xff) << 24);
int ret = ipHlpAPI.IcmpSendEcho(ptr, packed, data, (short) (dataLength), Pointer.NULL, icmpEchoReply, IcmpEchoReply.SIZE + dataLength, TIMEOUT);
if (ret != 1)
{
return -1;
}
return Math.toIntExact(icmpEchoReply.roundTripTime.longValue());
}
finally
{ {
ipHlpAPI.IcmpCloseHandle(ptr); ipHlpAPI.IcmpCloseHandle(ptr);
return -1;
} }
int rtt = Math.toIntExact(icmpEchoReply.roundTripTime.longValue());
ipHlpAPI.IcmpCloseHandle(ptr);
return rtt;
} }
private static int tcpPing(World world) throws IOException private static int tcpPing(World world) throws IOException