From e598f02cf87022c80340d046d99745914120099d Mon Sep 17 00:00:00 2001 From: Marius Hoch Date: Sat, 2 May 2015 18:48:04 +0200 Subject: [PATCH] Fix IP::toHex for IPv4 addresses with a double/triple 0 block Bug: T97897 Change-Id: I5c0a37be42ae2c5091ead487a6d19f6e0dd89b36 --- includes/utils/IP.php | 5 +++-- tests/phpunit/includes/utils/IPTest.php | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/utils/IP.php b/includes/utils/IP.php index 4441236..06389b2 100644 --- a/includes/utils/IP.php +++ b/includes/utils/IP.php @@ -395,8 +395,9 @@ class IP { if ( self::isIPv6( $ip ) ) { $n = 'v6-' . self::IPv6ToRawHex( $ip ); } elseif ( self::isIPv4( $ip ) ) { - // Bug 60035: an IP with leading 0's fails in ip2long sometimes (e.g. *.08) - $ip = preg_replace( '/(?<=\.)0+(?=[1-9])/', '', $ip ); + // T62035/T97897: An IP with leading 0's fails in ip2long sometimes (e.g. *.08), + // also double/triple 0 needs to be changed to just a single 0 for ip2long. + $ip = preg_replace( '/(?:^|(?<=\.))0+(?=[1-9]|0\.|0$)/', '', $ip ); $n = ip2long( $ip ); if ( $n < 0 ) { $n += pow( 2, 32 ); diff --git a/tests/phpunit/includes/utils/IPTest.php b/tests/phpunit/includes/utils/IPTest.php index acc9dfc..3a4f289 100644 --- a/tests/phpunit/includes/utils/IPTest.php +++ b/tests/phpunit/includes/utils/IPTest.php @@ -336,6 +336,7 @@ class IPTest extends PHPUnit_Framework_TestCase { array( '80000000', '128.0.0.0' ), array( 'DEADCAFE', '222.173.202.254' ), array( 'FFFFFFFF', '255.255.255.255' ), + array( '8D000BFD', '141.000.11.253' ), array( false, 'IN.VA.LI.D' ), array( 'v6-00000000000000000000000000000001', '::1' ), array( 'v6-20010DB885A3000000008A2E03707334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ), -- 2.3.2 (Apple Git-55)