Re: PHP 4.0 Bug #5404 Updated: String conversion to integer is incorrect
| From: | Kristo Iila | Date: | Wed, 05 Jul 2000 19:40:50 +0000 |
| Subject: | Re: PHP 4.0 Bug #5404 Updated: String conversion to integer is incorrect | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-23630@lists.php.net to get a copy of this message | ||
Good morning!
> ID: 5404
> Updated by: stas
> Reported By: kix@struktuur.ee
> Status: Open
> Bug Type: Scripting Engine problem
> Assigned To:
> Comments:
>
> It's not really PHP. It's C's strtod() function which is to blame. It
> does
> very strange tricks when it meets 0x-numbers.
> So, "0x2f" converted by strtod into 1072693248.
>
> Full Bug description available at: http://bugs.php.net/?id=5404
Yes, it is true that strtod behaves very strangely when it encounters a
hex number. But after looking at the sources of php it seems to me that
the check, whether the string is an integer or double not, is done in the
function is_numeric_string in the zend engine.
Well if it is, then it seems to me that, it is that function too which is
broken in the way it handles hexadecimal strings. The lines that check if
the string is an integer are these: (taken from Zend/zend_operators.h)
local_lval = strtol(str, &end_ptr, 10);
if (errno!=ERANGE && end_ptr == str+length) { /* integer string */
and if this check fails, then it is checked if the string is a double,
like this:
local_dval = strtod(str, &end_ptr);
if (errno!=ERANGE && end_ptr == str+length) { /* floating point string
The problem is that the base is specified when calling the strtol
function, as 10, leaving no room for finding octal or hex values. It seems
to me that if the base was specified as 0, in which case the function
tries to detect it itself, things would work quite nicely.
Or, if it is undesirable to convert hex strings to integers a check could
be added by trying to convert it like this, if the first conversion fails:
local_lval = strtol(str, &end_ptr, 16);
if (errno != ERANGE && end_ptr == str+length) { /* hex integer string */
and returning a 0 value, like php3 did in this case.
Kristo.