From 5f8e8bc70891bd08fceac3e25565a0f78e4129c7 Mon Sep 17 00:00:00 2001 From: Thalia Date: Wed, 8 May 2019 12:49:18 -0500 Subject: [PATCH] Fix Block::newLoad for IPv6 range blocks - follow-up to Ie8bebd8 Previously, the size of the range is calculated by finding the difference between that start and end addresses, converted into decimal. This fails when the numbers are too large. Instead, use parsing methods from IP class to avoid having to handle large numbers. Bug: T222246 Change-Id: If466139c42c4ac832a9506c80bdb228e9da39638 --- includes/Block.php | 10 +++++----- tests/phpunit/includes/BlockTest.php | 20 +++++++++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/includes/Block.php b/includes/Block.php index 472c36ed19..5b0d25611e 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -322,13 +322,13 @@ class Block extends AbstractBlock { if ( $block->getType() == self::TYPE_RANGE ) { # This is the number of bits that are allowed to vary in the block, give # or take some floating point errors - $prefix = 'v6-'; - $end = Wikimedia\base_convert( ltrim( $block->getRangeEnd(), $prefix ), 16, 10 ); - $start = Wikimedia\base_convert( ltrim( $block->getRangeStart(), $prefix ), 16, 10 ); - $size = log( $end - $start + 1, 2 ); + $target = $block->getTarget(); + $max = IP::isIPv6( $target ) ? 128 : 32; + list( $network, $bits ) = IP::parseCIDR( $target ); + $size = $max - $bits; # Rank a range block covering a single IP equally with a single-IP block - $score = self::TYPE_RANGE - 1 + ( $size / 128 ); + $score = self::TYPE_RANGE - 1 + ( $size / $max ); } else { $score = $block->getType(); diff --git a/tests/phpunit/includes/BlockTest.php b/tests/phpunit/includes/BlockTest.php index dac3b87732..3dc5e78433 100644 --- a/tests/phpunit/includes/BlockTest.php +++ b/tests/phpunit/includes/BlockTest.php @@ -175,21 +175,31 @@ class BlockTest extends MediaWikiLangTestCase { '0.0.0.0', '0.0.0.0' ], - 'Blocks to wide IPv6 range and IP' => [ - [ '0:0:0:0:0:0:0:0/19', '0:0:0:0:0:0:0:0' ], - '0:0:0:0:0:0:0:0', - '0:0:0:0:0:0:0:0' - ], 'Blocks to narrow IPv4 range and IP' => [ [ '0.0.0.0/31', '0.0.0.0' ], '0.0.0.0', '0.0.0.0' ], + 'Blocks to wide IPv6 range and IP' => [ + [ '0:0:0:0:0:0:0:0/19', '0:0:0:0:0:0:0:0' ], + '0:0:0:0:0:0:0:0', + '0:0:0:0:0:0:0:0' + ], 'Blocks to narrow IPv6 range and IP' => [ [ '0:0:0:0:0:0:0:0/127', '0:0:0:0:0:0:0:0' ], '0:0:0:0:0:0:0:0', '0:0:0:0:0:0:0:0' ], + 'Blocks to wide IPv6 range and IP, large numbers' => [ + [ '2000:DEAD:BEEF:A:0:0:0:0/19', '2000:DEAD:BEEF:A:0:0:0:0' ], + '2000:DEAD:BEEF:A:0:0:0:0', + '2000:DEAD:BEEF:A:0:0:0:0' + ], + 'Blocks to narrow IPv6 range and IP, large numbers' => [ + [ '2000:DEAD:BEEF:A:0:0:0:0/127', '2000:DEAD:BEEF:A:0:0:0:0' ], + '2000:DEAD:BEEF:A:0:0:0:0', + '2000:DEAD:BEEF:A:0:0:0:0' + ], ]; } -- 2.20.1