Skip to content
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

ARROW-16013: [C++][Python] Signed overflow when using negative stride in NumPyStridedConverter #12699

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cpp/src/arrow/python/numpy_to_arrow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,13 @@ class NumPyStridedConverter {
ARROW_ASSIGN_OR_RAISE(buffer_, AllocateBuffer(sizeof(T) * length_, pool_));

const int64_t stride = PyArray_STRIDES(arr)[0];
if (stride % sizeof(T) == 0) {
const int64_t stride_elements = stride / sizeof(T);
// ARROW-16013: convert sizeof(T) to signed int64 first, otherwise dividing by it
// would do an unsigned division. This cannot be caught by tests without ubsan, since
// common signed overflow behavior and the fact that the sizeof(T) is currently always
// a power of two here cause CopyStridedNatural to still produce correct results
const int64_t element_size = sizeof(T);
if (stride % element_size == 0) {
const int64_t stride_elements = stride / element_size;
CopyStridedNatural(reinterpret_cast<T*>(PyArray_DATA(arr)), length_,
stride_elements, reinterpret_cast<T*>(buffer_->mutable_data()));
} else {
Expand Down