Bug #53033 [Ver->Csd]: Mathematical operations convert objects to integers

From: Date: Tue, 26 Dec 2017 11:41:15 +0000
Subject: Bug #53033 [Ver->Csd]: Mathematical operations convert objects to integers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213275@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=53033&edit=1

 ID:                 53033
 Updated by:         nikic@php.net
 Reported by:        rowan dot collins at gmail dot com
 Summary:            Mathematical operations convert objects to integers
-Status:             Verified
+Status:             Closed
 Type:               Bug
 Package:            Class/Object related
 Operating System:   Any
 PHP Version:        5.3.3
-Assigned To:        
+Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

Fixed by https://github.com/php/php-src/commit/b2b2b437af5495dcb2016be6e45567ed826cb87e
in master.


Previous Comments:
------------------------------------------------------------------------
[2017-02-26 22:08:08] qheaden at phaseshiftsoftware dot com

Thanks for the info! It will be a big help.

------------------------------------------------------------------------
[2017-02-26 11:25:44] nikic@php.net

If you want to work on this, here's some pointers on what needs to be done. The general issue
is that cast_object handlers are currently limited to casting to integer or float, but not
"cast to integer/float, whichever is more appropriate". To fix this, a new casting mode
needs to be introduced. The procedure would be something like this:

* Add an _IS_NUMBER pseudo-type to zend_types.h
* In zend_operators.c, in convert_scalar_to_number() change the IS_OBJECT case to perform a cast to
_IS_NUMBER (the general code for this is similar as in convert_to_long_base). Similar adjustments
are necessary in zendi_convert_scalar_to_number().
* In sxe.c change the cast_object() function to handle the _IS_NUMBER cast (by calling
convert_scalar_to_number()).
* Check the codebase for other cast_object handlers and see if they need to be adjusted to support
casting to _IS_NUMBER.

------------------------------------------------------------------------
[2017-02-26 05:00:47] qheaden at phaseshiftsoftware dot com

It looks like this issue affects all PHP versions according to the following output: https://3v4l.org/OqOJq

I am willing to check this bug out.

------------------------------------------------------------------------
[2010-10-11 06:39:06] cataphract@php.net

read: (...) and PHP prefers a int (not a double).

By the way, the work-around is to ask for a double conversion explicitly: 

<?php
$x = simplexml_load_string('<x>2.5</x>');
var_dump((double)$x*1); // float(2.5)

------------------------------------------------------------------------
[2010-10-11 06:37:41] cataphract@php.net

This is tricky because there's no way to tell an object to cast itself to a numeric type
without asking explicitly for an int or a double and PHP prefers a double.

It would be possible to try first a double and, if failing, try an integer, but it's not
straightforward because the current functions/macros throw notices if the conversion fails...

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


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=53033


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


Thread (7 messages)

« previous php.bugs (#213275) next »