Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BCMath\Number operators may typecast operand #15968

Open
cmb69 opened this issue Sep 20, 2024 · 2 comments
Open

BCMath\Number operators may typecast operand #15968

cmb69 opened this issue Sep 20, 2024 · 2 comments

Comments

@cmb69
Copy link
Member

cmb69 commented Sep 20, 2024

Description

The following code:

<?php
class MyString {
    function __toString() {
        return "2";
    }
}

$a = new BCMath\Number("1");
$b = new MyString();
var_dump($a + $b, $b);

Resulted in this output:

object(BcMath\Number)#2 (2) {
  ["value"]=>
  string(1) "3"
  ["scale"]=>
  int(0)
}

string(1) "2"

But I expected this output instead:

object(BcMath\Number)#2 (2) {
  ["value"]=>
  string(1) "3"
  ["scale"]=>
  int(0)
}
object(MyString)#2 (0) {
}

Note that this even happens in strict typing mode, where it may not even be expected that the addition succeeds.

Anyhow, the problem is that zend_parse_arg_str_or_long_slow() converts the passed arg.

Also note that GMP also uses ZPP functionality, but catches any such type conversion, even in coercive typing mode.

PHP Version

PHP 8.4

Operating System

any

@cmb69
Copy link
Member Author

cmb69 commented Sep 21, 2024

Confirmed: https://3v4l.org/6g9B3/rfc#vgit.master

Somewhat different issue, but also related to bcmath_number_parse_num():

<?php declare(strict_types=0);
var_dump(new BCMath\Number("1") + null);

does not throw a deprecation warning, as it is supposed to, since the RFC states:

Calculations with the operator behave as if the corresponding method's optional arguments were not specified.

And for strict typing, the operation succeeds, but the method fails.

@iluuu1994
Copy link
Member

/cc @SakiTakamachi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants