|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
[2014-09-02 17:12 UTC] nikic@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: nikic
[2014-09-02 17:12 UTC] nikic@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 03:00:01 2025 UTC |
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