php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #63027 dechex may convert numbers up to PHP_INT_MAX
Submitted: 2012-09-07 08:53 UTC Modified: 2012-09-10 01:54 UTC
From: marc at ermshaus dot org Assigned: aharvey (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: 64-bit
Private report: No CVE-ID: None
 [2012-09-07 08:53 UTC] marc at ermshaus dot org
Description:
------------
The range of this function seems to be platform dependent and contain the whole range of the C long type.

The documentation states: “Returns a string containing a hexadecimal representation of the given number argument. The largest number that can be converted is 4294967295 in decimal resulting to "ffffffff".”

Technically, that would be the interval [-PHP_INT_MAX - 1, PHP_INT_MAX] as PHP uses unsigned longs.

Implementation details:

- dechex: https://github.com/php/php-src/blob/b47d6b32ba8d6dc1b9d1fc91a83eef29a28363c5/ext/standard/math.c#L1047

Calls:

- convert_to_long_ex: https://github.com/php/php-src/blob/b47d6b32ba8d6dc1b9d1fc91a83eef29a28363c5/Zend/zend_operators.h#L398
- convert_to_ex_master: https://github.com/php/php-src/blob/b47d6b32ba8d6dc1b9d1fc91a83eef29a28363c5/Zend/zend_operators.h#L355
- convert_to_long: https://github.com/php/php-src/blob/b47d6b32ba8d6dc1b9d1fc91a83eef29a28363c5/Zend/zend_operators.c#L324
- convert_to_long_base: https://github.com/php/php-src/blob/b47d6b32ba8d6dc1b9d1fc91a83eef29a28363c5/Zend/zend_operators.c#L332

Then strtol from stdlib would be called which returns a long.

Test script:
---------------
var_dump(
    PHP_INT_SIZE,
    PHP_INT_MAX,
    dechex(PHP_INT_MAX),
    dechex(-PHP_INT_MAX - 1),
    dechex(-1)
);

int(8)
int(9223372036854775807)
string(16) "7fffffffffffffff"
string(16) "8000000000000000"
string(16) "ffffffffffffffff"



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-07 09:44 UTC] marc at ermshaus dot org
“[…] as PHP uses unsigned longs.”

Sorry, that should be *signed* longs.
 [2012-09-10 01:54 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&revision=327571
Log: Fix doc bug #63027 (dechex may convert numbers up to PHP_INT_MAX) by clarifying
that PHP treats the parameter as an unsigned int, and working through some of
the implications of that from a userland perspective.
 [2012-09-10 01:54 UTC] aharvey@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: aharvey
 [2012-09-10 01:54 UTC] aharvey@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 13:01:28 2024 UTC