Skip to content

Conversation

@riku-sakamoto
Copy link
Contributor

@riku-sakamoto riku-sakamoto commented Jul 13, 2025

Fix to use Py_XINCREF instead of Py_INCREF to safely handle the case where op_dtypes[0] is NULL

Root Cause

  • When calling string_expandtabs_length_promoter, the first item of op_dtypes can be NULL.

Rationale

  • As noted in this comment regarding ops, legacy type resolution does not handle NULL well. Therefore, I changed the logic to use the second item instead.

Outcome

  • numpy._core.strings._expandtabs_length.reduce(numpy.zeros(200)) now raises a UFuncTypeError, instead of causing a segmentation fault.

@riku-sakamoto riku-sakamoto marked this pull request as draft July 13, 2025 16:55
@riku-sakamoto riku-sakamoto force-pushed the fix_expandtabs_length_segfault branch from 13d91c7 to 1aae38f Compare July 13, 2025 17:24
@ngoldbaum
Copy link
Member

ping @lysnikolaou

@riku-sakamoto riku-sakamoto marked this pull request as ready for review July 13, 2025 17:50
new_op_dtypes[0] = op_dtypes[0];
PyArray_DTypeMeta* _op_dtype = op_dtypes[0] ? op_dtypes[0] : op_dtypes[1];
Py_INCREF(_op_dtype);
new_op_dtypes[0] = _op_dtype;
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit of a ridiculous call anyway, since reducing doesn't make sense.

But if we want to put this in, please try with just making the old code an XINCREF instead should also work and makes more sense than something effectively random/wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your comment! I agree.
I fixed my code to use Py_XINCREF instead.

…gth.reduce`

Fixes the first item of numpy#28829. Use `Py_XINCREF` instead of `Py_INCREF` to safely handle the case where `op_dtypes[0]` is NULL
@riku-sakamoto riku-sakamoto force-pushed the fix_expandtabs_length_segfault branch from 1aae38f to 71a1502 Compare October 10, 2025 05:32
@mattip
Copy link
Member

mattip commented Oct 10, 2025

Please add a test

@riku-sakamoto
Copy link
Contributor Author

Please add a test

@mattip

Thank you for your check! I added a test.

@riku-sakamoto
Copy link
Contributor Author

riku-sakamoto commented Oct 10, 2025

CI tests on ARM64 are failing, and it seems to be related to the latest Python 3.11.14 release (Oct 9, 2025).
I'll investigate it.

@mattip
Copy link
Member

mattip commented Oct 10, 2025

I checked that the fix indeed prevents the segfault, and that the tests hit the desired codepath. Note that np._core.strings is not exposed in np._core.__all__.

@mattip mattip merged commit 6bb7264 into numpy:main Oct 10, 2025
74 of 77 checks passed
@mattip
Copy link
Member

mattip commented Oct 10, 2025

Thanks @riku-sakamoto

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Oct 10, 2025
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Oct 10, 2025
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.

5 participants