Repository navigation
BUG: spatial.distance: validate VI shape in pdist and cdist for metric='mahalanobis' - #26540
Merged
Merged
Conversation
gr-0x076
force-pushed
the
fix-mahalanobis-vi-shape-v2
branch
from
October 28, 2026 08:41
3f9b11e to
3b397f6
Compare
…c='mahalanobis' Validate that kwargs['VI'] has shape (n, n) in _validate_mahalanobis_kwargs, matching the observation dimension and preventing buffer over-reads and silent arithmetic errors in compiled distance kernels. Closes scipygh-26526
Contributor
|
This is now causing linting errors on main @j-bowhay |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference issue
Closes gh-26526 (Supersedes closed gh-26532 with required AI disclosure)
What does this implement/fix?
In
scipy.spatial.distance,pdistandcdistwithmetric='mahalanobis'did not validate that the inverse covariance matrixVIhas shape(n, n), wherenis the dimension of the observations.Because the underlying C kernel in
distance_impl.hlinearly indexescovinv + (i * n), passing an incompatible shape resulted in readingVIwith the wrong stride, while an undersized buffer resulted in reading past the allocated memory and terminating with a segmentation fault.This PR adds shape validation in
_validate_mahalanobis_kwargsensuringkwargs['VI'].shape == (n, n), consistent with the checks inscipy.spatial.distance.mahalanobis, and adds regression tests for bothcdistandpdist.Additional information
No compiled code is touched; this is pure Python parameter validation.
AI Generation Disclosure
AI tools (Antigravity) were used to assist with navigating the codebase, inspecting the C-level distance implementation, and drafting the shape check and pytest assertions. The mathematical requirements and regression tests were reviewed, verified, and run locally by the contributor before submission.