Skip to content

API,BUG: Fix scalar handling in array-interface allowing NULL pointers #29338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

seberg
Copy link
Member

@seberg seberg commented Jul 8, 2025

This fixes the scalar handling in the array-interface and also a bug in the shape handling error path.

In theory it breaks code that used NULL to trigger the scalar path, but this path was:

  1. Completely undocumented, and
  2. correctly triggered by ommitting the data field instead.

I didn't remove/deprecate the scalar path in this PR, maybe we should. But, I do think we can ignore that theoretical use-case since it is nonsensical.

Closes gh-26037

if (PyArray_SIZE(ret) > 1) {
PyErr_SetString(PyExc_ValueError,
"cannot coerce scalar to array with size > 1");
Py_DECREF(ret);
goto fail;
}
if (PyArray_SETITEM(ret, PyArray_DATA(ret), origin) < 0) {
if (PyArray_Pack(PyArray_DESCR(ret), PyArray_DATA(ret), origin) < 0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This probably doesn't matter in practice, but could matter for new dtypes in principle. It's just the right way now.

return NULL;
}
else if (result == 1) {
Py_DECREF(iface);
Py_DECREF(attr);
Copy link
Member Author

@seberg seberg Jul 8, 2025

Choose a reason for hiding this comment

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

This is a plain bug-fix, it could (and maybe should) be backported. In this branch attr is explicitly still NULL, since result == 0.

This fixes the scalar handling in the array-interface and also a bug
in the shape handling error path.

In theory it breaks code that used NULL to trigger the scalar path,
but this path was:
1. Completely undocumented, and
2. correctly triggered by *ommitting* the data field instead.

I didn't remove/deprecate the scalar path in this PR, maybe we should.
But, I do think we can ignore that theoretical use-case since it is
nonsensical.

Signed-off-by: Sebastian Berg <sebastianb@nvidia.com>
@seberg seberg force-pushed the array-interface-null branch from 9ec8b22 to 10af699 Compare July 8, 2025 11:23
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.

BUG: Cannot create zero-length array-like object via __array_interface__ providing null pointer as data
1 participant