Skip to content

BUG: spatial.distance: validate VI shape in pdist and cdist for metric='mahalanobis' - #26540

Merged
j-bowhay merged 1 commit into
scipy:mainfrom
gr-0x076:fix-mahalanobis-vi-shape-v2
Oct 28, 2026
Merged

j-bowhay merged 1 commit into
scipy:mainfrom
gr-0x076:fix-mahalanobis-vi-shape-v2

Conversation

@gr-0x076

Copy link
Copy Markdown
Contributor

Reference issue

Closes gh-26526 (Supersedes closed gh-26532 with required AI disclosure)

What does this implement/fix?

In scipy.spatial.distance, pdist and cdist with metric='mahalanobis' did not validate that the inverse covariance matrix VI has shape (n, n), where n is the dimension of the observations.

Because the underlying C kernel in distance_impl.h linearly indexes covinv + (i * n), passing an incompatible shape resulted in reading VI with 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_kwargs ensuring kwargs['VI'].shape == (n, n), consistent with the checks in scipy.spatial.distance.mahalanobis, and adds regression tests for both cdist and pdist.

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.

@github-actions github-actions Bot added scipy.spatial defect A clear bug or issue that prevents SciPy from being installed or used as expected labels Oct 28, 2026
@gr-0x076
gr-0x076 force-pushed the fix-mahalanobis-vi-shape-v2 branch from 3f9b11e to 3b397f6 Compare October 28, 2026 08:41
…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

@j-bowhay j-bowhay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@j-bowhay
j-bowhay merged commit 12a3744 into scipy:main Oct 28, 2026
59 of 60 checks passed
@j-bowhay j-bowhay added this to the 2.0.0 milestone Oct 28, 2026
@gr-0x076
gr-0x076 deleted the fix-mahalanobis-vi-shape-v2 branch October 28, 2026 13:05
@amacati

amacati commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

This is now causing linting errors on main @j-bowhay

@j-bowhay j-bowhay mentioned this pull request Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.spatial

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: spatial.distance.pdist and cdist do not validate VI shape for metric='mahalanobis'

3 participants