From 1409eb303100757bf061c895f7452f7b8d21878a 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 | 23 +++++++++++++++++++++-- tests/phpunit/includes/utils/IPTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/includes/utils/IP.php b/includes/utils/IP.php index 4441236..1bbbb32 100644 --- a/includes/utils/IP.php +++ b/includes/utils/IP.php @@ -381,6 +381,24 @@ class IP { } /** + * Remove leading 0's from octet representation of IPv4 address + * + * @param string $ip Quad dotted/octet IP address. + * @return string|bool False on failure + */ + public static function standardizeIPv4( $ip ) { + if ( self::isIPv4( $ip ) ) { + $ip = preg_replace( '/(?:^|(?<=\.))0+(?=[1-9]|0\.|0$)/', '', $ip ); + } + else { + $ip = false; + } + + return $ip; + + } + + /** * Return a zero-padded upper case hexadecimal representation of an IP address. * * Hexadecimal addresses are used because they can easily be extended to @@ -395,8 +413,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 = self::standardizeIPv4( $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..297b105 100644 --- a/tests/phpunit/includes/utils/IPTest.php +++ b/tests/phpunit/includes/utils/IPTest.php @@ -316,6 +316,34 @@ class IPTest extends PHPUnit_Framework_TestCase { } /** + * @covers IP::standardizeIPv4 + * @dataProvider provideStandardizeIPv4 + */ + public function testStandardizeIPv4( $expected, $input ) { + $result = IP::standardizeIPv4( $input ); + $this->assertTrue( $result === false || is_string( $result ) ); + $this->assertEquals( $expected, $result ); + } + + /** + * Provider for IP::testStandardizeIPv4() + */ + public static function provideStandardizeIPv4() { + return array( + array( '0.0.0.0', '0.0.0.0' ), + array( '0.0.0.0', '00.00.00.00' ), + array( '0.0.0.0', '000.000.000.000' ), + array( '141.0.11.253', '141.000.011.253' ), + array( '1.2.4.5', '1.2.4.5' ), + array( '1.2.4.5', '01.02.04.05' ), + array( '1.2.4.5', '001.002.004.005' ), + array( '10.0.0.1', '010.0.000.1' ), + array( '80.72.250.4', '080.072.250.04' ), + array( false, '080.072.0250.04' ) + ); + } + + /** * @covers IP::toHex * @dataProvider provideToHex */ @@ -336,6 +364,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)