This is an automated email from the ASF dual-hosted git repository.

pitrou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 98347d233f GH-50311: [C++] `KeyValueMetadata::Delete` returns 
IndexError instead of crashing due to seg fault (#50322)
98347d233f is described below

commit 98347d233f03bcf4d116d77f1769d498902b1fc8
Author: Om Biradar <[email protected]>
AuthorDate: Thu Jul 2 17:47:41 2026 +0530

    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 <[email protected]>
    Co-authored-by: Om Biradar <[email protected]>
    Co-authored-by: Antoine Pitrou <[email protected]>
    Signed-off-by: Antoine Pitrou <[email protected]>
---
 cpp/src/arrow/util/key_value_metadata.cc      |  6 ++++++
 cpp/src/arrow/util/key_value_metadata_test.cc | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/cpp/src/arrow/util/key_value_metadata.cc 
b/cpp/src/arrow/util/key_value_metadata.cc
index 48e02c6120..67bf02b4e1 100644
--- a/cpp/src/arrow/util/key_value_metadata.cc
+++ b/cpp/src/arrow/util/key_value_metadata.cc
@@ -100,6 +100,12 @@ Result<std::string> KeyValueMetadata::Get(std::string_view 
key) const {
 }
 
 Status KeyValueMetadata::Delete(int64_t index) {
+  if (ARROW_PREDICT_FALSE(index < 0 || index >= 
static_cast<int64_t>(keys_.size()))) {
+    return Status::IndexError("KeyValueMetadata::Delete: index ", index,
+                              " is out of bounds for metadata "
+                              "of size ",
+                              keys_.size());
+  }
   keys_.erase(keys_.begin() + index);
   values_.erase(values_.begin() + index);
   return Status::OK();
diff --git a/cpp/src/arrow/util/key_value_metadata_test.cc 
b/cpp/src/arrow/util/key_value_metadata_test.cc
index 3cdcf94757..7c021429ac 100644
--- a/cpp/src/arrow/util/key_value_metadata_test.cc
+++ b/cpp/src/arrow/util/key_value_metadata_test.cc
@@ -187,6 +187,26 @@ TEST(KeyValueMetadataTest, Delete) {
     ASSERT_OK(metadata.Delete(3));
     ASSERT_TRUE(metadata.Equals(
         KeyValueMetadata({"aa", "bb", "dd", "ff", "gg"}, {"1", "2", "4", "6", 
"7"})));
+
+    std::string expected_error_message =
+        "Index error: KeyValueMetadata::Delete: index -1 is out of bounds for 
metadata "
+        "of size 5";
+    ASSERT_RAISES_WITH_MESSAGE(IndexError, expected_error_message, 
metadata.Delete(-1));
+    expected_error_message =
+        "Index error: KeyValueMetadata::Delete: index 7 is out of bounds for 
metadata "
+        "of size 5";
+    ASSERT_RAISES_WITH_MESSAGE(IndexError, expected_error_message, 
metadata.Delete(7));
+
+    ASSERT_OK(metadata.Delete(4));
+    ASSERT_OK(metadata.Delete(3));
+    ASSERT_OK(metadata.Delete(2));
+    ASSERT_OK(metadata.Delete(1));
+    ASSERT_OK(metadata.Delete(0));
+
+    expected_error_message =
+        "Index error: KeyValueMetadata::Delete: index 7 is out of bounds for 
metadata "
+        "of size 0";
+    ASSERT_RAISES_WITH_MESSAGE(IndexError, expected_error_message, 
metadata.Delete(7));
   }
   {
     KeyValueMetadata metadata(keys, values);

Reply via email to