Skip to content

Handle possible NULL pointers in the array buffer #41

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

Closed
wants to merge 1 commit into from

Conversation

ngoldbaum
Copy link
Member

The discussion around numpy/numpy#23262 made me realize that there can be NULL pointers in the array buffer after np.zeros and np.empty. This means that we need to check for that case whenever we access an existing string array buffer in the casts and ufuncs.

To support this I added a new helper empty_if_null, which returns a new empty static string. I couldn't figure out how to do this in a way that avoids heap allocations because the ss struct uses a flexible array member, so I also need to free the resulting empty string later.

It would be possible to avoid this if there was a way for a dtype to supply an initial value for np.empty and np.zeros.

Alternative suggestions for how to handle this are very welcome.


if (*in == NULL) {
free(s);
}
Copy link
Member

@seberg seberg Feb 27, 2023

Choose a reason for hiding this comment

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

So, if we definitely don't do reference counting (each array that is not a view owns a copy of the string), then I do still think it makes sense to move the length information into the array data itself.

On this case, how about a pattern off:

    const ss *s = load_string(in);

And then return a statically allocated empty ss (or a once allocated global). That way you don't need the free() call. Freeing is only needed on store, and there you can do it unconditionally. The only thing is you must not free what get_string returns. I hope the const will protect a bit and it should at least be a nice hard crash that is easy to track down.

@ngoldbaum
Copy link
Member Author

Closing this in favor of a new PR implementing Sebastian's suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants