Author: fred
Description:
For example:
- converting '0x10' from base36 to base36 results in '000g' instead of '0x10'
- converting '0x1234567890abhaer' from base36 to base36 returns '000000000000000000, and raises this warning: "Warning: gmp_init(): Unable to convert variable to GMP - wrong type! in /home/fredemmott/test2.php on line 3"; presumably this is because 'h' isn't valid in an executable string
Tested with PHP 5.5.9 and HHVM 3.2.0 against Mediawiki master and 1.22wmf22
Isolated examples showing the cause:
<?php
$in = '0x1234567890abhaer';
$gmp = gmp_init($in, 36);
$val = gmp_strval($gmp, 36);
var_dump(str_pad($val, strlen($in), '0', STR_PAD_LEFT));
<?php
$in = '0x10';
$gmp = gmp_init($in, 36);
$val = gmp_strval($gmp, 36);
var_dump(str_pad($val, strlen($in), '0', STR_PAD_LEFT));
This issue was found by WfBaseConvertTest::testIdentity intermittently failing on HHVM's CI system since we added GMP support. Cause found by fuzzing:
<?php
function test() {
$chars = '0123456789abcdefghijklmnopqrstuvwxyz'; $base = mt_rand(2, 36); $len = mt_rand(10, 100); $str = ''; for ($i = 0; $i < $len; ++$i) { $str .= $chars[mt_rand(0, $base - 1)]; } $gmp = gmp_init($str, $base); $val = gmp_strval($str, $base); $val = str_pad($val, strlen($str), '0', STR_PAD_LEFT); if ($val !== $str) { throw new Exception('Not same - base '.$base."\n-".$str."\n+".$val); }
}
for($i = 0; $i < 100000; ++$i) {
test();
}
Version: 1.24rc
Severity: normal
OS: Linux
See Also:
https://bugs.php.net/bug.php?id=50175
https://bugs.php.net/bug.php?id=55398