Bug #55398 [Opn->Csd]: Leading 0s sometimes breaks GMP conversions

From: Date: Tue, 02 Sep 2014 17:12:01 +0000
Subject: Bug #55398 [Opn->Csd]: Leading 0s sometimes breaks GMP conversions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187388@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=55398&edit=1 ID: 55398 Updated by: nikic@php.net Reported by: marcus at synchromedia dot co dot uk Summary: Leading 0s sometimes breaks GMP conversions -Status: Open +Status: Closed Type: Bug Package: GNU MP related PHP Version: 5.3.6 -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: Fixed in PHP 5.6.1. Previous Comments: ------------------------------------------------------------------------ [2011-08-11 10:13:30] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=55398&edit=1

« previous php.bugs (#187388) next »