Workaround ip2long limitation
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 15 Jan 2014 03:58:03 +0000 (19:58 -0800)
committerBryanDavis <bdavis@wikimedia.org>
Fri, 17 Jan 2014 00:23:49 +0000 (00:23 +0000)
* This was resulting in bogus queries that did ipblock table scans

bug: 60035
Change-Id: Id8f8846e002abcc0010c8706c664db86257786bf

includes/utils/IP.php
tests/phpunit/includes/utils/IPTest.php

index 2686e11..871f71b 100644 (file)
@@ -489,6 +489,8 @@ class IP {
                if ( self::isIPv6( $ip ) ) {
                        $n = self::toUnsigned6( $ip );
                } else {
+                       // Bug 60035: an IP with leading 0's fails in ip2long sometimes (e.g. *.08)
+                       $ip = preg_replace( '/(?<=\.)0+(?=[1-9])/', '', $ip );
                        $n = ip2long( $ip );
                        if ( $n < 0 ) {
                                $n += pow( 2, 32 );
index 24b077a..1267268 100644 (file)
@@ -297,6 +297,8 @@ class IPTest extends MediaWikiTestCase {
                        array( 16909060, '1.2.3.4' ),
                        array( 2130706433, '127.0.0.1' ),
                        array( '2147483648', '128.0.0.0' ),
+                       array( 2130706440, '127.0.0.08' ),
+                       array( 2130706441, '127.0.0.09' ),
                        array( '3735931646', '222.173.202.254' ),
                        array( pow( 2, 32 ) - 1, '255.255.255.255' ),
                        array( false, 'IN.VA.LI.D' ),