Skip to content

Segmentation fault (index oob) in ext/bcmath/libbcmath/src/convert.c:155 #16265

Closed

Description

@YuanchengJiang

Description

The following code:

<?php
$variation_array = array(
'float -12.3456789000e10' => -12.3456789000e10,
);
$fusion = $variation_array;
require(__DIR__ . "/run_bcmath_tests_function.inc");
$dividends = ["15", "-15", "1", "-9", "14.14", "-16.60", "0.15", "-0.01"];
run_bcmath_tests($dividends, $fusion, "/", bcdiv(...));

Resulted in this output:

/php-src/ext/bcmath/libbcmath/src/convert.c:155:68: runtime error: index 8791595 out of bounds for type 'const unsigned char[100]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /php-src/ext/bcmath/libbcmath/src/convert.c:155:68

PHP Version

PHP 8.4.0-dev

Operating System

ubuntu 22.04

Activity

  1. ndossche commented on Oct 6, 2024

    @ndossche
    Member

    Likely same root cause as #16262

  2. cmb69 commented on Oct 6, 2024

    @cmb69
    Member

    Simplified reproducer:

    bcdiv("-0.01", -12.3456789000e10, 9);
  3. ndossche commented on Oct 6, 2024

    @ndossche
    Member

    No, slightly different cause than #16262.
    The problem here is that quot_real_arr_size is 0, so the loop for (i = 0; i < quot_real_arr_size - 1; i++) { goes OOB.
    However, the loop must write bytes because otherwise the numbers in bc_num are uninitialized.

  4. added a commit that references this issue on Nov 4, 2024
    2fe7719
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