Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9532f298 authored by Narayan Kamath's avatar Narayan Kamath
Browse files

DataConnection: Use InetAddress.isNumeric rather than a regex.

android.util.Patterns is pretty heavy weight; it can take a long
time to initialize and can increase java heap usage (dirty memory)
by hundreds of kilobytes. InetAddress.isNumeric provides the same
functionality using getaddrinfo(..AI_NUMERIC) and friends, and
provides a guarantee that the resulting string will be parseable
by InetAddress.getByName and friends can parse it correctly.

Bug: 63107244
Test: DataConnectionTest

Change-Id: I9078aea83dd2da621576ff3328b975bd65c00b72
parent 39443c82
Loading
Loading
Loading
Loading
+7 −3
Original line number Original line Diff line number Diff line
@@ -36,9 +36,9 @@ import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.Pair;
import android.util.Pair;
import android.util.Patterns;
import android.util.TimeUtils;
import android.util.TimeUtils;


import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.telephony.CallTracker;
import com.android.internal.telephony.CallTracker;
import com.android.internal.telephony.CarrierSignalAgent;
import com.android.internal.telephony.CarrierSignalAgent;
import com.android.internal.telephony.CommandException;
import com.android.internal.telephony.CommandException;
@@ -976,10 +976,14 @@ public class DataConnection extends StateMachine {
        return result;
        return result;
    }
    }


    private boolean isIpAddress(String address) {
    /**
     * @return {@code} true iff. {@code address} is a literal IPv4 or IPv6 address.
     */
    @VisibleForTesting
    public static boolean isIpAddress(String address) {
        if (address == null) return false;
        if (address == null) return false;


        return Patterns.IP_ADDRESS.matcher(address).matches();
        return InetAddress.isNumeric(address);
    }
    }


    private DataCallResponse.SetupResult setLinkProperties(DataCallResponse response,
    private DataCallResponse.SetupResult setLinkProperties(DataCallResponse response,
+12 −1
Original line number Original line Diff line number Diff line
@@ -314,4 +314,15 @@ public class DataConnectionTest extends TelephonyTest {
                .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED));
                .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED));
        assertFalse(getNetworkInfo().isMetered());
        assertFalse(getNetworkInfo().isMetered());
    }
    }

    @SmallTest
    public void testIsIpAddress() throws Exception {
        // IPv4
        assertTrue(DataConnection.isIpAddress("1.2.3.4"));
        assertTrue(DataConnection.isIpAddress("127.0.0.1"));

        // IPv6
        assertTrue(DataConnection.isIpAddress("::1"));
        assertTrue(DataConnection.isIpAddress("2001:4860:800d::68"));
    }
}
}