From 5957856c46c3c351b88b6f150ccd39afb5be2c5c Mon Sep 17 00:00:00 2001 From: Kevin Israel Date: Tue, 2 Sep 2014 15:33:43 -0400 Subject: [PATCH] wfBaseConvert(): Work around PHP Bug #50175 Before php/php-src@834daa455bc9, PHP's gmp_init() function would try to autodetect hex and binary numbers even when a base was explicitly specified[1], so the results for some base-36 numbers having leading zeros could be incorrect. Work around this bug by trimming off any leading zeros before calling gmp_init(). [1]: https://bugs.php.net/bug.php?id=50175 Bug: 69249 Change-Id: I5f5458c1a1195f55fa12904c103da6ea7558010a --- RELEASE-NOTES-1.24 | 1 + includes/GlobalFunctions.php | 5 ++++- .../phpunit/includes/GlobalFunctions/wfBaseConvertTest.php | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/RELEASE-NOTES-1.24 b/RELEASE-NOTES-1.24 index c5b6cd45fd..039cf7090f 100644 --- a/RELEASE-NOTES-1.24 +++ b/RELEASE-NOTES-1.24 @@ -209,6 +209,7 @@ production. * (bugs 57238, 65206) Blank pages can now be directly created. * (bug 69789) Title::getContentModel() now loads from the database when necessary instead of incorrectly returning the default content model. +* (bug 69249) wfBaseConvert() now works around PHP Bug #50175 when using GMP. === Action API changes in 1.24 === * action=parse API now supports prop=modules, which provides the list of diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index ddea620b81..cfe9a87dc1 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -3366,7 +3366,10 @@ function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1, ); if ( extension_loaded( 'gmp' ) && ( $engine == 'auto' || $engine == 'gmp' ) ) { - $result = gmp_strval( gmp_init( $input, $sourceBase ), $destBase ); + // Removing leading zeros works around broken base detection code in + // some PHP versions (see and + // ). + $result = gmp_strval( gmp_init( ltrim( $input, '0' ), $sourceBase ), $destBase ); } elseif ( extension_loaded( 'bcmath' ) && ( $engine == 'auto' || $engine == 'bcmath' ) ) { $decimal = '0'; foreach ( str_split( strtolower( $input ) ) as $char ) { diff --git a/tests/phpunit/includes/GlobalFunctions/wfBaseConvertTest.php b/tests/phpunit/includes/GlobalFunctions/wfBaseConvertTest.php index dd4f9ccfa4..9d55e85c81 100644 --- a/tests/phpunit/includes/GlobalFunctions/wfBaseConvertTest.php +++ b/tests/phpunit/includes/GlobalFunctions/wfBaseConvertTest.php @@ -186,4 +186,10 @@ class WfBaseConvertTest extends MediaWikiTestCase { strlen( wfBaseConvert( $number, 2, 2, strlen( $number ) - 5 ) ) ); } + + public function testLeadingZero() { + $this->assertSame( '24', wfBaseConvert( '010', 36, 16 ) ); + $this->assertSame( '37d4', wfBaseConvert( '0b10', 36, 16 ) ); + $this->assertSame( 'a734', wfBaseConvert( '0x10', 36, 16 ) ); + } } -- 2.20.1