Skip to content

Commit d4420ab

Browse files
author
Robert Greenwalt
committed
Trim leading zeros from ipv4 addrs.
Underlying libraries will interpret leading zeros as octal values and fail. bug:5262995 Change-Id: Iff949225bb6b941f7274ee81754e1f41ed719a6c
1 parent 74b496d commit d4420ab

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

core/java/android/net/NetworkUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,31 @@ public static String[] makeStrings(Collection<InetAddress> addrs) {
250250
}
251251
return result;
252252
}
253+
254+
/**
255+
* Trim leading zeros from IPv4 address strings
256+
* Our base libraries will interpret that as octel..
257+
* Must leave non v4 addresses and host names alone.
258+
* For example, 192.168.000.010 -> 192.168.0.10
259+
* TODO - fix base libraries and remove this function
260+
* @param addr a string representing an ip addr
261+
* @return a string propertly trimmed
262+
*/
263+
public static String trimV4AddrZeros(String addr) {
264+
String[] octets = addr.split("\\.");
265+
if (octets.length != 4) return addr;
266+
StringBuilder builder = new StringBuilder(16);
267+
String result = null;
268+
for (int i = 0; i < 4; i++) {
269+
try {
270+
if (octets[i].length > 3) return addr;
271+
builder.append(Integer.parseInt(octets[i]));
272+
} catch (NumberFormatException e) {
273+
return addr;
274+
}
275+
if (i < 3) builder.append('.');
276+
}
277+
result = builder.toString();
278+
return result;
279+
}
253280
}

telephony/java/com/android/internal/telephony/gsm/GsmDataConnectionTracker.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,10 +914,16 @@ private ArrayList<ApnSetting> createApnList(Cursor cursor) {
914914
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NUMERIC)),
915915
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.NAME)),
916916
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.APN)),
917-
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PROXY)),
917+
NetworkUtils.trimV4AddrZeros(
918+
cursor.getString(
919+
cursor.getColumnIndexOrThrow(Telephony.Carriers.PROXY))),
918920
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PORT)),
919-
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSC)),
920-
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPROXY)),
921+
NetworkUtils.trimV4AddrZeros(
922+
cursor.getString(
923+
cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSC))),
924+
NetworkUtils.trimV4AddrZeros(
925+
cursor.getString(
926+
cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPROXY))),
921927
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.MMSPORT)),
922928
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.USER)),
923929
cursor.getString(cursor.getColumnIndexOrThrow(Telephony.Carriers.PASSWORD)),

0 commit comments

Comments
 (0)