From ed6abd6b0463f22997b2df4d51f3e0596c0f88a7 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 14 Jan 2014 19:58:03 -0800 Subject: [PATCH] Workaround ip2long limitation * This was resulting in bogus queries that did ipblock table scans bug: 60035 Change-Id: Id8f8846e002abcc0010c8706c664db86257786bf --- includes/utils/IP.php | 2 ++ tests/phpunit/includes/utils/IPTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/includes/utils/IP.php b/includes/utils/IP.php index 2686e1133c..871f71bcdc 100644 --- a/includes/utils/IP.php +++ b/includes/utils/IP.php @@ -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 ); diff --git a/tests/phpunit/includes/utils/IPTest.php b/tests/phpunit/includes/utils/IPTest.php index 24b077a3f5..1267268364 100644 --- a/tests/phpunit/includes/utils/IPTest.php +++ b/tests/phpunit/includes/utils/IPTest.php @@ -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' ), -- 2.20.1