NumPower / numpower

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

[BUG] new instance with invalid parameters problem #58

Closed SkibidiProduction closed 2 months ago

SkibidiProduction commented 2 months ago

When running the following code:

new NDArray();

I expect to receive:

Fatal error: Uncaught ArgumentCountError: NDArray::__construct() expects exactly 1 argument, 0 given

Instead I get a segmentation fault:

Segmentation fault (core dumped)

The same problem occurs if I try to pass an empty array or invalid value:

new NDArray([]);
//Segmentation fault (core dumped)

new NDArray('string');
//Segmentation fault (core dumped)

If we talk about the problem of passing an empty array, then in #54 and #56 I processed this case and threw an exception. However, now I think that perhaps this is not quite the right approach and we should give the user the opportunity to create instances of the class from an empty array.

henrique-borba commented 2 months ago

After testing with valgrind, I identified that the error occurred when calling the destructor. It appears that PHP calls the destructor before dealing with __construct method exceptions. The destructor was trying to free an NDArray that did not exist.

Fixed by https://github.com/NumPower/numpower/pull/59

henrique-borba commented 2 months ago

The constructor is already supporting an empty array now, but I found that the __toString method and print_r are not able to handle empty arrays. I will correct this in another MR.

henrique-borba commented 2 months ago

Empty array initialization fixed in https://github.com/NumPower/numpower/pull/60