From 3a0d14cb122041b019eaa857950bfc8fb48a047d Mon Sep 17 00:00:00 2001 From: Wes McKinney Date: Sat, 13 Jun 2020 21:58:18 -0500 Subject: [PATCH] Test 0-length case --- cpp/src/arrow/array/array_binary.h | 8 ++++++-- cpp/src/arrow/array/array_binary_test.cc | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/array/array_binary.h b/cpp/src/arrow/array/array_binary.h index e27a2bfcc5e..b291de3ab72 100644 --- a/cpp/src/arrow/array/array_binary.h +++ b/cpp/src/arrow/array/array_binary.h @@ -105,8 +105,12 @@ class BaseBinaryArray : public FlatArray { /// referenced by this array. If the array has been sliced then this may be /// less than the size of the data buffer (data_->buffers[2]). offset_type total_values_length() const { - return raw_value_offsets_[data_->length + data_->offset] - - raw_value_offsets_[data_->offset]; + if (data_->length > 0) { + return raw_value_offsets_[data_->length + data_->offset] - + raw_value_offsets_[data_->offset]; + } else { + return 0; + } } protected: diff --git a/cpp/src/arrow/array/array_binary_test.cc b/cpp/src/arrow/array/array_binary_test.cc index 41945e552ff..f20f0ea3c8c 100644 --- a/cpp/src/arrow/array/array_binary_test.cc +++ b/cpp/src/arrow/array/array_binary_test.cc @@ -119,6 +119,11 @@ class TestStringArray : public ::testing::Test { offset_type sliced_values_length = checked_cast(*arr.Slice(3)).total_values_length(); ASSERT_EQ(sliced_values_length, static_cast(9)); + + // Zero-length array is a special case + offset_type zero_size_length = + checked_cast(*arr.Slice(0, 0)).total_values_length(); + ASSERT_EQ(zero_size_length, static_cast(0)); } void TestType() {