Skip to content

Fix Coverity OVERRUN false positive in _allocate_result - #364

Merged
antonwolfy merged 1 commit into
masterfrom
fix/coverity-allocate-result-overrun
Aug 10, 2026
Merged

Fix Coverity OVERRUN false positive in _allocate_result#364
antonwolfy merged 1 commit into
masterfrom
fix/coverity-allocate-result-overrun

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes a Coverity out-of-bounds access (OVERRUN) finding reported in __pyx_f_7mkl_fft_7_pydfti__allocate_result.

The finding is a false positive, but it stems from a real code-quality issue: f_ndim in _allocate_result was left untyped, so it was a Python object rather than a C int.

Root cause

Because f_ndim was a Python object, the expression f_ndim * sizeof(cnp.npy_intp) was compiled as a Python-level multiplication (PyNumber_Multiply) followed by __Pyx_PyLong_As_size_t. That conversion returns the error sentinel (size_t)-1 (18446744073709551615) on failure. Coverity explored the path where the sentinel reaches PyMem_Malloc/memcpy without a Python exception being set, and reported an out-of-bounds write on f_shape.

That state is unreachable at runtime — f_ndim is a NumPy array's number of dimensions (0–64) — but the analyzer cannot prove it once the value is laundered through a PyObject.

Tellingly, the structurally identical memcpy in _pad_array was not flagged, because its b_ndim is already declared cdef int.

Fix

Declare f_ndim as cdef int, matching b_ndim in _pad_array. The size is then computed as a plain C multiplication, and the __Pyx_PyLong_As_size_t sentinel path disappears entirely from the generated C.

Declare f_ndim as a C int so that f_ndim * sizeof(npy_intp) is computed
as a plain C multiplication instead of being routed through a Python
object. The untyped variable caused Cython to generate a
__Pyx_PyLong_As_size_t conversion whose (size_t)-1 error sentinel led
Coverity to report an out-of-bounds access (OVERRUN, CID 652776/652762)
on the PyMem_Malloc/memcpy of f_shape. This mirrors b_ndim in
_pad_array, which is already typed and was never flagged.
@antonwolfy antonwolfy added this to the 2.4.0 release milestone Aug 10, 2026
@antonwolfy antonwolfy self-assigned this Aug 10, 2026
@antonwolfy
antonwolfy marked this pull request as ready for review August 10, 2026 10:29
@antonwolfy

Copy link
Copy Markdown
Collaborator Author

The issue is resolved on the branch:
https://scan8.scan.coverity.com/#/project-view/68095/16416

@vlad-perevezentsev vlad-perevezentsev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thank you @antonwolfy

@antonwolfy
antonwolfy merged commit c3ccd3c into master Aug 10, 2026
62 checks passed
@antonwolfy
antonwolfy deleted the fix/coverity-allocate-result-overrun branch August 10, 2026 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants