Skip to content

Commit

Permalink
ARROW-9116: [C++][FOLLOWUP] Add 0-length test for BaseBinaryArray::to…
Browse files Browse the repository at this point in the history
…tal_values_length

This also protects against the offsets buffer being null.

Closes #7431 from wesm/ARROW-9116-followup

Authored-by: Wes McKinney <wesm@apache.org>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
wesm authored and kou committed Jun 14, 2020
1 parent 657d9c6 commit 3ccecfb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cpp/src/arrow/array/array_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions cpp/src/arrow/array/array_binary_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ class TestStringArray : public ::testing::Test {
offset_type sliced_values_length =
checked_cast<const ArrayType&>(*arr.Slice(3)).total_values_length();
ASSERT_EQ(sliced_values_length, static_cast<offset_type>(9));

// Zero-length array is a special case
offset_type zero_size_length =
checked_cast<const ArrayType&>(*arr.Slice(0, 0)).total_values_length();
ASSERT_EQ(zero_size_length, static_cast<offset_type>(0));
}

void TestType() {
Expand Down

0 comments on commit 3ccecfb

Please sign in to comment.