Bug #54973 [Bgs]: SimpleXML casts intergers wrong.

From: Date: Thu, 02 Jun 2011 20:14:15 +0000
Subject: Bug #54973 [Bgs]: SimpleXML casts intergers wrong.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-161140@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=54973&edit=1

 ID:                 54973
 Updated by:         dtajchreber@php.net
 Reported by:        bphelpsen at gmail dot com
 Summary:            SimpleXML casts intergers wrong.
 Status:             Bogus
 Type:               Bug
 Package:            SimpleXML related
 Operating System:   Linux
 PHP Version:        5.3.6
 Block user comment: N
 Private report:     N

 New Comment:

This looks like a bug... SimpleXML's cast_object handler doesn't check for 
overflow when trying to convert a node value to a 
long. $xml->value goes through Zend's add_function and gets passed to 
zendi_convert_scalar_to_number which calls SimpleXML's 
cast_object handler because $xml->number is an object. It then gets chopped into 
LONG_MAX by strtol. An overflow check could 
fix this... but it might break some BC.... 

[1] http://lxr.php.net/xref/PHP_5_3/Zend/zend_operators.c#758
[2] http://lxr.php.net/xref/PHP_5_3/ext/simplexml/simplexml.c#1722
[3] http://lxr.php.net/xref/PHP_5_3/Zend/zend_operators.c#355
[4] http://lxr.php.net/xref/PHP_5_3/Zend/zend_operators.c#768
[5] http://www.gnu.org/s/hello/manual/libc/Parsing-of-Integers.html


Previous Comments:
------------------------------------------------------------------------
[2011-06-02 20:05:07] bphelpsen at gmail dot com

Then why do I get different results when I run this code:

<?php echo "214748364800" / 1024 / 1024 / 1024; ?>

than when I run the example code.  Shouldn't they return the same value?

Here is an example, with the same results as my local test: 

http://codepad.org/KICl3xHw

------------------------------------------------------------------------
[2011-06-02 20:00:54] iliaa@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

This has nothing to do with SimpleXML, but rather the fact that a numeric string 
in PHP by default would converted to an integer.

------------------------------------------------------------------------
[2011-06-01 22:18:27] bphelpsen at gmail dot com

Description:
------------
When an XML node value is an integer SimpleXML always casts it to an int, even if its a float or
double.

Test script:
---------------
<?php
$xml =
simplexml_load_string("<xml><number>214748364800</number></xml>");

echo $xml->number . "\n"; // the proper number

$int = $xml->number / 1024 / 1024 / 1024; // initial cast to an int causes problems
echo $int . "\n"; 

$double = (double) $xml->number / 1024 / 1024 / 1024; // hard cast to a double fixes it
echo $double . "\n";
?>

Expected result:
----------------
214748364800
200
200

Actual result:
--------------
214748364800
1.9999999990687
200


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=54973&edit=1


Thread (10 messages)

« previous php.bugs (#161140) next »