NumPower / numpower

PHP extension for efficient scientific computing and array manipulation with GPU support
https://numpower.org
Other
185 stars 4 forks source link

fix: segmentation fault with empty arrays initialization and __toString magic method #60

Closed henrique-borba closed 2 months ago

henrique-borba commented 2 months ago

Fixed an issue where initializing empty ndarrays caused segmentation errors:

$a = nd::array([]);

Results:

Segmentation fault (core dumped)

This also prepares the methods print_r (__toString) and toArray to use empty arrays. With this MR the code below will succefully execute:

$a = nd::array([]);
$a->dump();
print_r($a);
echo $a;
print_r($a->toArray());

__toString segmentation fault

The __toString method was incorrectly freeing memory and causing segmentation fault when trying to read an array using echo.

PHP_METHOD(NDArray, __toString) {
    ...
    char *result = NDArray_Print(ndarray, 1);
    RETVAL_STRING(result);
    efree(result); << caused segfault
}