| 714 | |
| 715 | template <typename ListType> |
| 716 | Status ValidateListLike(const ListType& type) { |
| 717 | const ArrayData& values = *data.child_data[0]; |
| 718 | const Status child_valid = RecurseInto(values); |
| 719 | if (!child_valid.ok()) { |
| 720 | return Status::Invalid("List child array invalid: ", child_valid.ToString()); |
| 721 | } |
| 722 | |
| 723 | // First validate offsets, to make sure the accesses below are valid |
| 724 | RETURN_NOT_OK(ValidateOffsetsAndSizes(type, values.offset + values.length)); |
| 725 | |
| 726 | // An empty list array can have 0 offsets |
| 727 | if (data.length > 0 && data.buffers[1]->is_cpu()) { |
| 728 | using offset_type = typename ListType::offset_type; |
| 729 | |
| 730 | const auto offsets = data.GetValues<offset_type>(1); |
| 731 | |
| 732 | const auto first_offset = offsets[0]; |
| 733 | const auto last_offset = offsets[data.length]; |
| 734 | // This early test avoids undefined behaviour when computing `data_extent` |
| 735 | if (first_offset < 0 || last_offset < 0) { |
| 736 | return Status::Invalid("Negative offsets in list array"); |
| 737 | } |
| 738 | const auto data_extent = last_offset - first_offset; |
| 739 | const auto values_length = values.length; |
| 740 | if (values_length < data_extent) { |
| 741 | return Status::Invalid("Length spanned by list offsets (", data_extent, |
| 742 | ") larger than values array (length ", values_length, ")"); |
| 743 | } |
| 744 | // These tests ensure that array concatenation is safe if Validate() succeeds |
| 745 | // (for delta dictionaries) |
| 746 | if (first_offset > values_length || last_offset > values_length) { |
| 747 | return Status::Invalid("First or last list offset out of bounds"); |
| 748 | } |
| 749 | if (first_offset > last_offset) { |
| 750 | return Status::Invalid("First offset larger than last offset in list array"); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | return Status::OK(); |
| 755 | } |
| 756 | |
| 757 | template <typename ListViewType> |
| 758 | Status ValidateListView(const ListViewType& type) { |