Skip to content

Commit 98347d2

Browse files
OmBiradarpitrou
andauthored
GH-50311: [C++] KeyValueMetadata::Delete returns IndexError instead of crashing due to seg fault (#50322)
* The Delete function now catches out of bound index and does not throw a segmentation fault. ### Rationale for this change The `KeyValueMetadata::Delete(int64_t index)` never checked for out of bounds value of `index`, & if `index` was out of bounds, then a seg fault was thrown by the program and it aborted. ### What changes are included in this PR? The `KeyValueMetadata::Delete(int64_t index)` now returns a IndexError for out of bounds values of `index` ### Are these changes tested? Yes, CI tests pass on my fork ### Are there any user-facing changes? No API changes * GitHub Issue: #50311 Lead-authored-by: OmBiradar <ombiradar04@gmail.com> Co-authored-by: Om Biradar <ombiradar04@gmail.com> Co-authored-by: Antoine Pitrou <pitrou@free.fr> Signed-off-by: Antoine Pitrou <antoine@python.org>
1 parent 0f6dd60 commit 98347d2

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

cpp/src/arrow/util/key_value_metadata.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ Result<std::string> KeyValueMetadata::Get(std::string_view key) const {
100100
}
101101

102102
Status KeyValueMetadata::Delete(int64_t index) {
103+
if (ARROW_PREDICT_FALSE(index < 0 || index >= static_cast<int64_t>(keys_.size()))) {
104+
return Status::IndexError("KeyValueMetadata::Delete: index ", index,
105+
" is out of bounds for metadata "
106+
"of size ",
107+
keys_.size());
108+
}
103109
keys_.erase(keys_.begin() + index);
104110
values_.erase(values_.begin() + index);
105111
return Status::OK();

cpp/src/arrow/util/key_value_metadata_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,26 @@ TEST(KeyValueMetadataTest, Delete) {
187187
ASSERT_OK(metadata.Delete(3));
188188
ASSERT_TRUE(metadata.Equals(
189189
KeyValueMetadata({"aa", "bb", "dd", "ff", "gg"}, {"1", "2", "4", "6", "7"})));
190+
191+
std::string expected_error_message =
192+
"Index error: KeyValueMetadata::Delete: index -1 is out of bounds for metadata "
193+
"of size 5";
194+
ASSERT_RAISES_WITH_MESSAGE(IndexError, expected_error_message, metadata.Delete(-1));
195+
expected_error_message =
196+
"Index error: KeyValueMetadata::Delete: index 7 is out of bounds for metadata "
197+
"of size 5";
198+
ASSERT_RAISES_WITH_MESSAGE(IndexError, expected_error_message, metadata.Delete(7));
199+
200+
ASSERT_OK(metadata.Delete(4));
201+
ASSERT_OK(metadata.Delete(3));
202+
ASSERT_OK(metadata.Delete(2));
203+
ASSERT_OK(metadata.Delete(1));
204+
ASSERT_OK(metadata.Delete(0));
205+
206+
expected_error_message =
207+
"Index error: KeyValueMetadata::Delete: index 7 is out of bounds for metadata "
208+
"of size 0";
209+
ASSERT_RAISES_WITH_MESSAGE(IndexError, expected_error_message, metadata.Delete(7));
190210
}
191211
{
192212
KeyValueMetadata metadata(keys, values);

0 commit comments

Comments
 (0)