From 92304baa2c8e08b43deda73c1c0952109eeb0bba Mon Sep 17 00:00:00 2001 From: gary-wzl77 Date: Wed, 13 Oct 2021 20:53:00 +0800 Subject: [PATCH] net: fix NPE when reading IP configurations When the default gateway is not used in IP configurations, for example the gateway info in ip configurations was fed with custom gateway address ``` out.writeUTF(GATEWAY_KEY); out.writeInt(0); // Default route. out.writeInt(1); // Have a gateway. out.writeUTF(staticIpConfiguration.getGateway().getHostAddress()); ``` A NPE occurred. When there is no destination, assume it is default route and use the gateway address in all cases. Test: manual Change-Id: I5904efad5d277de6724f81d99e62c21ff8347caa Signed-off-by: gary-wzl77 --- .../core/java/com/android/server/net/IpConfigStore.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/net/IpConfigStore.java b/services/core/java/com/android/server/net/IpConfigStore.java index df1eb6d9fe..d17dbde496 100644 --- a/services/core/java/com/android/server/net/IpConfigStore.java +++ b/services/core/java/com/android/server/net/IpConfigStore.java @@ -322,8 +322,11 @@ public class IpConfigStore { gateway = InetAddresses.parseNumericAddress(in.readUTF()); } // If the destination is a default IPv4 route, use the gateway - // address unless already set. - if (dest.getAddress() instanceof Inet4Address + // address unless already set. If there is no destination, assume + // it is default route and use the gateway address in all cases. + if (dest == null) { + gatewayAddress = gateway; + } else if (dest.getAddress() instanceof Inet4Address && dest.getPrefixLength() == 0 && gatewayAddress == null) { gatewayAddress = gateway; } else {