Handle closed socket and NPE properly

Don't get interfaceIndex if the socket is closed. Properly catch NPE
instead of letting is propagating.

Test: TH
Change-Id: If962541e67dd6323426e46bc7a1f118786f83b9b
This commit is contained in:
Yuyang Huang
2023-09-11 13:07:53 +09:00
parent 8df4d76aa2
commit 8f4adaedaf

View File

@@ -128,9 +128,13 @@ public class MdnsSocket {
* cannot be determined, returns -1.
*/
public int getInterfaceIndex() {
if (multicastSocket.isClosed()) {
sharedLog.e("Socket is closed");
return -1;
}
try {
return multicastSocket.getNetworkInterface().getIndex();
} catch (SocketException e) {
} catch (SocketException | NullPointerException e) {
sharedLog.e("Failed to retrieve interface index for socket.", e);
return -1;
}