wfBaseConvert(): Work around PHP Bug #50175
authorKevin Israel <pleasestand@live.com>
Tue, 2 Sep 2014 19:33:43 +0000 (15:33 -0400)
committerKevin Israel <pleasestand@live.com>
Tue, 2 Sep 2014 20:02:27 +0000 (16:02 -0400)
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
includes/GlobalFunctions.php
tests/phpunit/includes/GlobalFunctions/wfBaseConvertTest.php

index c5b6cd4..039cf70 100644 (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
index ddea620..cfe9a87 100644 (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 ) {
index dd4f9cc..9d55e85 100644 (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 ) );
+       }
 }