Skip to content

Conversation

@AliRana30
Copy link

@AliRana30 AliRana30 commented Jan 31, 2026

Rationale for This Change

The SparseCSFIndex::Equals method can crash when comparing two sparse indices that have a different number of dimensions. The method iterates over the indices() and indptr() vectors of the current object and accesses the corresponding elements in the other object without first verifying that both objects have matching vector sizes. This can lead to out-of-bounds access and a segmentation fault when the dimension counts differ.

What Changes Are Included in This PR?

This change adds explicit size equality checks for the indices() and indptr() vectors at the beginning of the SparseCSFIndex::Equals method. If the dimensions do not match, the method now safely returns false instead of attempting invalid memory access.

Are These Changes Tested?

Yes. The fix has been validated through targeted reproduction of the crash scenario using mismatched dimension counts, ensuring the method behaves safely and deterministically.

Are There Any User-Facing Changes?

No. This change improves internal safety and robustness without altering public APIs or observable user behavior.

Closes

Closes #49104

GitHub Issue: [C++] Segfault in SparseCSFIndex::Equals with mismatched dimensions #49104

@github-actions
Copy link

Thanks for opening a pull request!

If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose

Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project.

Then could you also rename the pull request title in the following format?

GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}

or

MINOR: [${COMPONENT}] ${SUMMARY}

See also:

@AliRana30 AliRana30 changed the title [C++] Fix Segfault in SparseCSFIndex::Equals with mismatched dimensions GH-49104: [C++] Fix Segfault in SparseCSFIndex::Equals with mismatched dimensions Jan 31, 2026
@github-actions
Copy link

⚠️ GitHub issue #49104 has been automatically assigned in GitHub to PR creator.

@kou
Copy link
Member

kou commented Feb 1, 2026

Could you add a test for this case?

@AliRana30 AliRana30 force-pushed the fix-sparsecsfindex-equals-segfault branch from d60ea08 to 3e1cbd6 Compare February 1, 2026 13:50
@AliRana30
Copy link
Author

@kou On your request, I have added a new test case, TestEqualityMismatchedDimensions, in
cpp/src/arrow/sparse_tensor_test.cc.

Test details:
This test compares SparseCSFIndex objects with mismatched dimensions (for example, 1D vs 2D) to verify that Equals now safely returns false instead of causing a segfault.

Fix summary:
The fix adds explicit checks for the sizes of indices, indptr, and axis_order before attempting to iterate over them, ensuring safe comparison when dimensions do not match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] Segfault in SparseCSFIndex::Equals with mismatched dimensions

3 participants