Skip to content

GH-51019: [Python] Raise IndexError for out-of-bounds KeyValueMetadata key/value - #51020

Open
waterWang wants to merge 1 commit into
apache:mainfrom
waterWang:fix/kvm-index-error-51019
Open

GH-51019: [Python] Raise IndexError for out-of-bounds KeyValueMetadata key/value#51020
waterWang wants to merge 1 commit into
apache:mainfrom
waterWang:fix/kvm-index-error-51019

Conversation

@waterWang

@waterWang waterWang commented Aug 28, 2026

Copy link
Copy Markdown

Rationale for this change

KeyValueMetadata.key(i) / .value(i) pass the index through to the C++
KeyValueMetadata::key/value accessors, whose DCHECK bounds checks are
compiled out in release builds. Any out-of-range or negative index — most
easily hit with pa.KeyValueMetadata().key(0) on an empty object — performs
an unchecked std::vector::operator[] access and segfaults the Python
process
instead of raising an exception. Reproduced locally on pyarrow
25.0.1.

What changes are included in this PR?

Bounds checks in the Python bindings (types.pxi) raise IndexError for
negative or out-of-range indexes, matching the behavior of other indexed
pyarrow containers.

Are these changes tested?

Yes — added test_key_value_metadata_index_errors covering in-bounds
access, out-of-range/negative indexes, and empty-metadata access. The
segfault reproduces on pyarrow 25.0.1 (SIGSEGV); with the fix the same
calls raise IndexError.

…etadata key/value

KeyValueMetadata.key(i)/value(i) forwarded the index straight to the
C++ KeyValueMetadata::key/value, whose bounds DCHECKs are compiled out in
release builds. An out-of-range or negative index (e.g. on an empty
metadata object, as reported) therefore performed an unchecked
std::vector::operator[] access and segfaulted the process instead of
raising a Python exception.

Add an explicit bounds check in the Python bindings so negative and
out-of-range indexes raise IndexError, matching the behavior of other
indexed pyarrow containers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] KeyValueMetadata.key() segfaults on an out-of-range index

1 participant