GH-51044: [Python] Reject read-only readinto destinations - #51126
Merged
raulcd merged 2 commits intoSep 9, 2026
Conversation
Immutable destinations expose a null mutable pointer and can crash the native read path. Validate destination mutability and cover bytes and read-only memoryviews with regression tests. Assisted-by: OpenAI Codex
carrerasdarren-cell
requested review from
AlenkaF,
raulcd and
rok
as code owners
September 1, 2026 18:33
raulcd
approved these changes
Sep 8, 2026
raulcd
left a comment
Member
There was a problem hiding this comment.
I've reproduced this locally with 25.0.1, the fix seems reasonable to me:
$ python
Python 3.14.6 (main, Jun 10 2026, 18:54:31) [GCC 15.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyarrow as pa
>>> dst_buf=memoryview(b"a")
>>> with pa.BufferReader(b"x") as f:
... f.readinto(dst_buf)
...
Segmentation fault python
Member
AlenkaF
approved these changes
Sep 9, 2026
AlenkaF
left a comment
Member
There was a problem hiding this comment.
Just a minor nit: can we move the test after test_nativefile_write_memoryview under the Buffers section in the same file?
Otherwise LGTM, thanks!
Move the existing decorated test immediately after test_nativefile_write_memoryview, as requested in review. AI-assisted mechanical relocation only; test logic and decorators are unchanged. Checked Python syntax, statement and byte equivalence, and whitespace. Runtime tests were not rerun for this ordering change.
Member
|
Only did a quick look, but LGTM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
BufferReader.readinto()currently segfaults when passed a read-only destination such asbytesor a read-onlymemoryview.py_buffer()creates an immutable Arrow buffer,mutable_data()returns a null pointer, and the C++ read path attempts to copy into that pointer. A Python API misuse should raise a Python exception rather than terminate the interpreter.What changes are included in this PR?
NativeFile.readinto()destination is mutable before obtaining its writable pointer.TypeErrorfor immutable destinations.bytesand read-onlymemoryviewdestinations.Are these changes tested?
Yes. The focused
readintotests pass against a locally compiled patchedpyarrow.lib. The stock 25.0.1 wheel exits with status 139 for the issue reproducer, while the patched build raises the expectedTypeError; the writablebytearraycontrol continues to read successfully.Are there any user-facing changes?
Yes. Passing a read-only destination to
NativeFile.readinto()now raisesTypeErrorinstead of terminating the interpreter. Writable-buffer behavior is unchanged.Fixes #51044.
This contribution was developed with assistance from OpenAI Codex. I reviewed, tested, and take responsibility for the patch and this description.
BufferReader.readinto()segfaults on a read-only destination #51044