From 619319a3f560099b10d3a410f7241537bf9a0fcb Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Fri, 15 Jan 2021 11:06:21 +0800 Subject: [PATCH] Replace hidden ParcelFileDescriptor constructor usage If IOException happens while trying to start keepalives sockets, an invalid fd will be constructed. It will fail with ERROR_INVALID_SOCKET if the user later calls start(). Current design to construct the invalid fd use the hidden ParcelFileDescriptor constructor which will not work for the incoming ConnectivityService mainline. Thus, replace it with the other formal API. Bug: 170598012 Test: atest FrameworksNetTests Change-Id: I57fd7ec2281c5e8c75481200bbde723ecf96982a --- core/java/android/net/ConnectivityManager.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index 06c159804a..b3ca55e3e7 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -69,7 +69,6 @@ import com.android.internal.util.Protocol; import libcore.net.event.NetworkEventDispatcher; -import java.io.FileDescriptor; import java.io.IOException; import java.io.UncheckedIOException; import java.lang.annotation.Retention; @@ -1955,6 +1954,12 @@ public class ConnectivityManager { return k; } + // Construct an invalid fd. + private ParcelFileDescriptor createInvalidFd() { + final int invalidFd = -1; + return ParcelFileDescriptor.adoptFd(invalidFd); + } + /** * Request that keepalives be started on a IPsec NAT-T socket. * @@ -1985,7 +1990,7 @@ public class ConnectivityManager { } catch (IOException ignored) { // Construct an invalid fd, so that if the user later calls start(), it will fail with // ERROR_INVALID_SOCKET. - dup = new ParcelFileDescriptor(new FileDescriptor()); + dup = createInvalidFd(); } return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source, destination, executor, callback); @@ -2027,7 +2032,7 @@ public class ConnectivityManager { } catch (IOException ignored) { // Construct an invalid fd, so that if the user later calls start(), it will fail with // ERROR_INVALID_SOCKET. - dup = new ParcelFileDescriptor(new FileDescriptor()); + dup = createInvalidFd(); } return new NattSocketKeepalive(mService, network, dup, INVALID_RESOURCE_ID /* Unused */, source, destination, executor, callback); @@ -2064,7 +2069,7 @@ public class ConnectivityManager { } catch (UncheckedIOException ignored) { // Construct an invalid fd, so that if the user later calls start(), it will fail with // ERROR_INVALID_SOCKET. - dup = new ParcelFileDescriptor(new FileDescriptor()); + dup = createInvalidFd(); } return new TcpSocketKeepalive(mService, network, dup, executor, callback); }