From: Operating system: PHP version: 5.3.6 Package: GNU MP related Bug Type: Bug Bug description:Leading 0s sometimes breaks GMP conversions
Description: ------------ gmp_init() accepts a numeric string and an optional base paramater. The string can use PHP's standard notation for bases - leading 0 or octal, leading 0x for hex, but this can be overridden by the base parameter. To an extent it does this, but if the input value matches one of those patterns, but is not valid in octal or hex, the conversion fails. It's not quite that simple, as a string that looks like invalid octal is successfully handled as hex, though it doesn't work in other bases. I think this is a bug - if the base parameter is supplied, it should take complete precedence over the string format, no matter what it is. I think this is a PHP issue rather than a gmp library issue, since it's affected by PHP's numeric conventions that gmp doesn't share. It's possible to work around this by trimming leading zeros from the input values, however that shouldn't be necessary, and at the very least should be documented if it's needed. Test script: --------------- echo gmp_strval(gmp_init('010'), 16)."\n"; //Octal echo gmp_strval(gmp_init('010', 16), 16)."\n"; //Looks like octal, treat as b16 echo gmp_strval(gmp_init('01f'), 16)."\n"; //Invalid octal echo gmp_strval(gmp_init('01f', 16), 16)."\n"; //Looks like invalid octal, treat as b16 echo gmp_strval(gmp_init('10'), 16)."\n"; //Decimal echo gmp_strval(gmp_init('10', 16), 16)."\n"; //Looks like decimal, treat as b16 echo gmp_strval(gmp_init('0x10'), 16)."\n"; //Hex echo gmp_strval(gmp_init('0x10', 16), 16)."\n"; //Looks like hex, is hex echo gmp_strval(gmp_init('0x1h'), 16)."\n"; //Invalid hex echo gmp_strval(gmp_init('0x10', 62), 16)."\n"; //Looks like hex, treat as b62 echo gmp_strval(gmp_init('0x1h', 62), 16)."\n"; //Looks like invalid hex, treat as b62 echo gmp_strval(gmp_init('x10', 62), 16)."\n"; //Looks like ?, treat as b62 echo gmp_strval(gmp_init('0Bz', 62), 16)."\n"; //Looks like invalid octal, treat as b62 echo gmp_strval(gmp_init('Bz', 62), 16)."\n"; //Looks like ?, treat as b62 Expected result: ---------------- 8 10 0 1f a 10 10 10 0 10 3762a 3762a 2e7 2e7 Actual result: -------------- 8 10 0 1f a 10 10 10 0 10 0 3762a 0 2e7 -- Edit bug report at https://bugs.php.net/bug.php?id=55398&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=55398&r=trysnapshot54 Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=55398&r=trysnapshot53 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=55398&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=55398&r=fixed Fixed in SVN and need be documented: https://bugs.php.net/fix.php?id=55398&r=needdocs Fixed in release: https://bugs.php.net/fix.php?id=55398&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=55398&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=55398&r=needscript Try newer version: https://bugs.php.net/fix.php?id=55398&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=55398&r=support Expected behavior: https://bugs.php.net/fix.php?id=55398&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=55398&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=55398&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=55398&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=55398&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=55398&r=dst IIS Stability: https://bugs.php.net/fix.php?id=55398&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=55398&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=55398&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=55398&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=55398&r=mysqlcfg