Skip to content

Conversation

@WarrenWeckesser
Copy link
Member

@WarrenWeckesser WarrenWeckesser commented Oct 10, 2021

Before this change, the line in convert_datatype.c

result = PyArray_CastDescrToDType(all_descriptors[0], common_dtype);

would trigger a seg. fault with a call such as

np.result_type(0, np.timedelta64(4))

The problem was that all_descriptors[0] was NULL, and it was
dereferenced in PyArray_CastDescrToDType.

The code in the loop that followed the above line avoided passing
NULL values in all_descriptors[i] to PyArray_CastDescrToDType by
checking that the corresponding input argument to result_type
was not a Python scalar. A different code path was taken in that
case. The seg. fault is fixed by applying that same check to the
first argument.

Closes gh-20077.

@WarrenWeckesser
Copy link
Member Author

I don't know what went wrong with the Travis CI Python 3.10-dev job, but I suspect it is not related to this PR. The log has just one line:

An error occurred while generating the build script.

Before this change, the line in convert_datatype.c

    result = PyArray_CastDescrToDType(all_descriptors[0], common_dtype);

would trigger a seg. fault with a call such as

    np.result_type(0, np.timedelta64(4))

The problem was that `all_descriptors[0]` was NULL, and it was
dereferenced in PyArray_CastDescrToDType.

The code in the loop that followed the above line avoided passing
NULL values in all_descriptors[i] to PyArray_CastDescrToDType by
checking that the corresponding input argument to `result_type`
was not a Python scalar.  A different code path was taken in that
case.  The seg. fault is fixed by applying that same check to the
first argument.

Closes numpygh-20077.
@WarrenWeckesser
Copy link
Member Author

Force-pushed a trivial change. All the tests pass--I guess the Travis CI issue was a transient one.

@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Oct 11, 2021
@seberg seberg added this to the 1.21.3 release milestone Oct 11, 2021
Copy link
Member

@seberg seberg left a comment

Choose a reason for hiding this comment

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

Thanks, annoying complexity here with that value-based promotion/casting :(. An obvious fix though, since it just extends the ugly logic from above.

My only comment is to try to be a bit more clear that this helper only makes sense in the context of np.result_type

* See that function for the meaning and contents of the parameters.
*/
static PyArray_Descr *
get_dtype_from_descr_and_common(
Copy link
Member

Choose a reason for hiding this comment

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

Maybe rename to get_descr_from_cast_or_value? But not sure.

Copy link
Member

Choose a reason for hiding this comment

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

(I am tempted to overindent the arguments, but it is probably just me who does that...)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@charris charris merged commit 8c9cf79 into numpy:main Oct 11, 2021
@charris
Copy link
Member

charris commented Oct 11, 2021

Thanks Warren.

@WarrenWeckesser WarrenWeckesser deleted the fix-gh-200077 branch October 11, 2021 19:46
charris added a commit to charris/numpy that referenced this pull request Oct 12, 2021
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Oct 12, 2021
@charris charris removed this from the 1.21.3 release milestone Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: np.result_type segfault on timedelta64 and int

3 participants