scipy: BUG: special: Unchecked calls of malloc() and calloc() in specfun.h

Describe your issue.

Scattered throughout https://github.com/scipy/scipy/blob/main/scipy/special/xsf/specfun/specfun.h are calls of malloc() and calloc() where the return values are used without being checked for failure. That will cause a seg. fault if an allocation does ever fail, which is not correct behavior. Failures of memory allocation must ultimately raise a MemoryError.

We did a lot of work a few years ago to clean up such issues (https://github.com/scipy/scipy/issues/9999). Perhaps it is time for another audit.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (21 by maintainers)

Most upvoted comments

Keeping it strictly to the dynamic allocation, if we allocate beforehand as much as we can before we send things to ufunc rabbit hole, and leave the remaining a few with big fat warnings, I think that’s already a big accomplishment. We can then try to find out what the good way would be for the remaining errors.

That’s my laziness when I was converting fortran stack array allocations.

Yeah, I understand! But eventually the technical debt must be paid.

It looks like this occurs in C++ functions with void return type. They could be changed to return an int status code (eg. 0 means OK, -1 means memory allocation failed). (Or, as @steppi mentioned above, a pointer to a status code could be added as a parameter.) The wrappers must check this and propagate the error code up the wrapping layers until the top level wrapper can detect it and set a Python MemoryError exception.