Merge "Fix socket leaks in various android.net.Network methods."

am: 34ce11f2bd

Change-Id: I1a59fced1f3bcd19bd48150ca215093dff431619
This commit is contained in:
Paul Jensen
2018-03-30 22:03:22 +00:00
committed by android-build-merger

View File

@@ -25,6 +25,8 @@ import android.system.OsConstants;
import com.android.okhttp.internalandroidapi.Dns; import com.android.okhttp.internalandroidapi.Dns;
import com.android.okhttp.internalandroidapi.HttpURLConnectionFactory; import com.android.okhttp.internalandroidapi.HttpURLConnectionFactory;
import libcore.io.IoUtils;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramSocket; import java.net.DatagramSocket;
@@ -136,9 +138,15 @@ public class Network implements Parcelable {
for (int i = 0; i < hostAddresses.length; i++) { for (int i = 0; i < hostAddresses.length; i++) {
try { try {
Socket socket = createSocket(); Socket socket = createSocket();
if (localAddress != null) socket.bind(localAddress); boolean failed = true;
socket.connect(new InetSocketAddress(hostAddresses[i], port)); try {
return socket; if (localAddress != null) socket.bind(localAddress);
socket.connect(new InetSocketAddress(hostAddresses[i], port));
failed = false;
return socket;
} finally {
if (failed) IoUtils.closeQuietly(socket);
}
} catch (IOException e) { } catch (IOException e) {
if (i == (hostAddresses.length - 1)) throw e; if (i == (hostAddresses.length - 1)) throw e;
} }
@@ -155,15 +163,27 @@ public class Network implements Parcelable {
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, public Socket createSocket(InetAddress address, int port, InetAddress localAddress,
int localPort) throws IOException { int localPort) throws IOException {
Socket socket = createSocket(); Socket socket = createSocket();
socket.bind(new InetSocketAddress(localAddress, localPort)); boolean failed = true;
socket.connect(new InetSocketAddress(address, port)); try {
socket.bind(new InetSocketAddress(localAddress, localPort));
socket.connect(new InetSocketAddress(address, port));
failed = false;
} finally {
if (failed) IoUtils.closeQuietly(socket);
}
return socket; return socket;
} }
@Override @Override
public Socket createSocket(InetAddress host, int port) throws IOException { public Socket createSocket(InetAddress host, int port) throws IOException {
Socket socket = createSocket(); Socket socket = createSocket();
socket.connect(new InetSocketAddress(host, port)); boolean failed = true;
try {
socket.connect(new InetSocketAddress(host, port));
failed = false;
} finally {
if (failed) IoUtils.closeQuietly(socket);
}
return socket; return socket;
} }
@@ -175,7 +195,13 @@ public class Network implements Parcelable {
@Override @Override
public Socket createSocket() throws IOException { public Socket createSocket() throws IOException {
Socket socket = new Socket(); Socket socket = new Socket();
bindSocket(socket); boolean failed = true;
try {
bindSocket(socket);
failed = false;
} finally {
if (failed) IoUtils.closeQuietly(socket);
}
return socket; return socket;
} }
} }