Repository: arrow Updated Branches: refs/heads/master 8febd03f8 -> c3a122e1c
ARROW-939: fix division by zero if one of the tensor dimensions is zero This was reported and fixed by @stephanie-wang, see https://github.com/ray-project/ray/issues/500 Author: Philipp Moritz <[email protected]> Closes #634 from pcmoritz/master and squashes the following commits: 399681b [Philipp Moritz] fix linting 44ee13a [Philipp Moritz] fix strides if one of the tensor dimensions is zero 4d831ed [Philipp Moritz] fix division by zero if one of the tensor dimensions is zero Project: http://git-wip-us.apache.org/repos/asf/arrow/repo Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/c3a122e1 Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/c3a122e1 Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/c3a122e1 Branch: refs/heads/master Commit: c3a122e1cbe83028531bfd73f9a4e1401031c824 Parents: 8febd03 Author: Philipp Moritz <[email protected]> Authored: Sat May 6 10:56:00 2017 -0400 Committer: Wes McKinney <[email protected]> Committed: Sat May 6 10:56:00 2017 -0400 ---------------------------------------------------------------------- cpp/src/arrow/tensor-test.cc | 10 ++++++++++ cpp/src/arrow/tensor.cc | 11 +++++++++++ 2 files changed, 21 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/arrow/blob/c3a122e1/cpp/src/arrow/tensor-test.cc ---------------------------------------------------------------------- diff --git a/cpp/src/arrow/tensor-test.cc b/cpp/src/arrow/tensor-test.cc index c41683a..0a11422 100644 --- a/cpp/src/arrow/tensor-test.cc +++ b/cpp/src/arrow/tensor-test.cc @@ -93,4 +93,14 @@ TEST(TestTensor, IsContiguous) { ASSERT_FALSE(t3.is_contiguous()); } +TEST(TestTensor, ZeroDimensionalTensor) { + std::vector<int64_t> shape = {0}; + + std::shared_ptr<MutableBuffer> buffer; + ASSERT_OK(AllocateBuffer(default_memory_pool(), 0, &buffer)); + + Tensor t(int64(), buffer, shape); + ASSERT_EQ(t.strides().size(), 1); +} + } // namespace arrow http://git-wip-us.apache.org/repos/asf/arrow/blob/c3a122e1/cpp/src/arrow/tensor.cc ---------------------------------------------------------------------- diff --git a/cpp/src/arrow/tensor.cc b/cpp/src/arrow/tensor.cc index 909b05e..bcd9d8d 100644 --- a/cpp/src/arrow/tensor.cc +++ b/cpp/src/arrow/tensor.cc @@ -41,6 +41,11 @@ static void ComputeRowMajorStrides(const FixedWidthType& type, remaining *= dimsize; } + if (remaining == 0) { + strides->assign(shape.size(), type.bit_width() / 8); + return; + } + for (int64_t dimsize : shape) { remaining /= dimsize; strides->push_back(remaining); @@ -51,6 +56,12 @@ static void ComputeColumnMajorStrides(const FixedWidthType& type, const std::vector<int64_t>& shape, std::vector<int64_t>* strides) { int64_t total = type.bit_width() / 8; for (int64_t dimsize : shape) { + if (dimsize == 0) { + strides->assign(shape.size(), type.bit_width() / 8); + return; + } + } + for (int64_t dimsize : shape) { strides->push_back(total); total *= dimsize; }
