Bug #73108 [NEW]: Internal class cast handler uses integer instead of float
From: hartenfels at uni-koblenz dot de
Operating system: Linux
PHP version: 7.0.11
Package: Class/Object related
Bug Type: Bug
Bug description:Internal class cast handler uses integer instead of float
Description:
------------
This is a problem related to cast_object handlers for internal classes.
When using an object with a cast handler in arithmetic, integer
conversion is triggered instead of floating point conversion, even in
situations where floating point math is used.
The expected behavior would be that the floating point cast is triggered
instead.
If only a cast handler for floating point is present, the conversion
even fails outright with the message that the object could not be
converted to an integer. The cast to double isn't even attempted.
This may be related to #54973.
Test script:
---------------
The entire extension can be found at
https://github.com/hartenfels/castbug
static int castbug_cast(zval *readobj, zval *retval, int type
TSRMLS_DC)
{
if (type == IS_LONG) {
ZVAL_LONG(retval, 123);
return SUCCESS;
}
else if (type == IS_DOUBLE) {
ZVAL_DOUBLE(retval, 31.41);
return SUCCESS;
}
else {
return FAILURE;
}
}
<?php
$cb = new CastBug();
/* these work as expected */
var_dump((int) $cb);
var_dump((float) $cb);
/* these use integer conversion instead of double conversion */
var_dump($cb + 1.5);
var_dump($cb * 2.0);
var_dump($cb / 10.0);
Expected result:
----------------
int(123)
float(31.41)
float(32.91)
float(62.82)
float(3.141)
Actual result:
--------------
int(123)
float(31.41)
float(124.5)
float(246)
float(12.3)
--
Edit bug report at https://bugs.php.net/bug.php?id=73108&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=73108&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=73108&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=73108&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=73108&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=73108&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=73108&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=73108&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=73108&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=73108&r=support
Expected behavior: https://bugs.php.net/fix.php?id=73108&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=73108&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=73108&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=73108&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73108&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=73108&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=73108&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=73108&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=73108&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=73108&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=73108&r=mysqlcfg
Thread (6 messages)
- hartenfels at uni-koblenz dot de