php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55398 Leading 0s sometimes breaks GMP conversions
Submitted: 2011-08-11 10:13 UTC Modified: 2014-09-02 17:12 UTC
From: marcus at synchromedia dot co dot uk Assigned: nikic (profile)
Status: Closed Package: GNU MP related
PHP Version: 5.3.6 OS:
Private report: No CVE-ID: None
 [2011-08-11 10:13 UTC] marcus at synchromedia dot co dot uk
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

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
Fixed in PHP 5.6.1.
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Nov 19 13:01:26 2018 UTC