Skip to content

Segmentation fault (access null pointer) in ext/bcmath/libbcmath/src/rmzero.c:50 #16236

Closed

Description

@YuanchengJiang

Description

The following code:

<?php
$values = ['12.5', '-12.5'];
$exponents = [
[new BcMath\Number('-2'), 'object'],
];
foreach ($values as $fusion) {
$num = new BcMath\Number($value);
foreach ($exponents as [$exponent, $type]) {
$ret = $num->pow($exponent);
}
}

Resulted in this output:

/php-src/ext/bcmath/libbcmath/src/rmzero.c:50:11: runtime error: member access within null pointer of type 'struct bc_struct'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /php-src/ext/bcmath/libbcmath/src/rmzero.c:50:11

PHP Version

PHP 8.4.0-dev

Operating System

ubuntu 22.04

Activity

  1. cmb69 commented on Oct 5, 2024

    @cmb69
    Member

    Simpler reproducer:

    (new BcMath\Number("0"))->pow("-2");

    If bc_raise() fails, it apparently doesn't change result, so this needs to be checked prior to dereferencing it.

    Interestingly, https://3v4l.org/YmJUi https://3v4l.org/YmJUi/rfc#output

  2. cmb69 commented on Oct 5, 2024

    @cmb69
    Member

    If bc_raise() fails, it apparently doesn't change result, so this needs to be checked prior to dereferencing it.

    Or bc_init_num() the variable to get the same doubtful result as with bcpow().

  3. SakiTakamachi commented on Oct 7, 2024

    @SakiTakamachi
    Member

    Thank you for finding!
    I resolved that, will open PR later.

  4. added a commit that references this issue on Nov 19, 2024
    2c8662d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions