From 175f436f4882cedf1942bbce5b718f3d39264c70 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 20 Dec 2020 12:54:22 -0500 Subject: [PATCH] ping: place IcmpCloseHandle in finally If toIntExact() throws then this would leak the handle --- .../client/plugins/worldhopper/ping/Ping.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/ping/Ping.java b/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/ping/Ping.java index fd9a84fa57..42c88d4975 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/ping/Ping.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/worldhopper/ping/Ping.java @@ -66,26 +66,29 @@ public class Ping { IPHlpAPI ipHlpAPI = IPHlpAPI.INSTANCE; Pointer ptr = ipHlpAPI.IcmpCreateFile(); - InetAddress inetAddress = InetAddress.getByName(world.getAddress()); - byte[] address = inetAddress.getAddress(); - String dataStr = RUNELITE_PING; - int dataLength = dataStr.length() + 1; - Pointer data = new Memory(dataLength); - data.setString(0L, dataStr); - IcmpEchoReply icmpEchoReply = new IcmpEchoReply(new Memory(IcmpEchoReply.SIZE + dataLength)); - assert icmpEchoReply.size() == IcmpEchoReply.SIZE; - 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) + try + { + InetAddress inetAddress = InetAddress.getByName(world.getAddress()); + byte[] address = inetAddress.getAddress(); + String dataStr = RUNELITE_PING; + int dataLength = dataStr.length() + 1; + Pointer data = new Memory(dataLength); + data.setString(0L, dataStr); + IcmpEchoReply icmpEchoReply = new IcmpEchoReply(new Memory(IcmpEchoReply.SIZE + dataLength)); + assert icmpEchoReply.size() == IcmpEchoReply.SIZE; + 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); - return -1; } - - int rtt = Math.toIntExact(icmpEchoReply.roundTripTime.longValue()); - ipHlpAPI.IcmpCloseHandle(ptr); - - return rtt; } private static int tcpPing(World world) throws IOException