wfBaseConvert(): Work around PHP Bug #50175

Before php/php-src@834daa455b, 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
This commit is contained in:
Kevin Israel 2014-09-02 15:33:43 -04:00
parent da83f3e91b
commit 5957856c46
3 changed files with 11 additions and 1 deletions

View file

@ -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

View file

@ -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 <https://bugs.php.net/bug.php?id=50175> and
// <https://bugs.php.net/bug.php?id=55398>).
$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 ) {

View file

@ -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 ) );
}
}