Skip to content

gh-161691: Fix use of released memoryview in comparison and 'c' item assignment - #161738

Open
alonfaraj wants to merge 2 commits into
python:mainfrom
alonfaraj:gh-161691
Open

alonfaraj wants to merge 2 commits into
python:mainfrom
alonfaraj:gh-161691

Conversation

@alonfaraj

@alonfaraj alonfaraj commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes: #161691

Memoryview could still be used after Python code released it in the middle of an operation. Two spots were missing the re-check that gh-92888 added elsewhere:

  • m == obj where obj.__buffer__ releases m: we tested m before calling PyObject_GetBuffer on the other side and never again after. Now if m got released we just say not equal, same as any other released view.
  • m[key] = b"x" with format 'c', where key.__index__ releases m: every other format re-checks after converting the value, but 'c' doesn't convert anything, so it never re-checked. I added the same CHECK_RELEASED_INT_AGAIN there.

On main this doesn't segfault anymore, since gh-127716 keeps the buffer alive while the operation runs. You still get wrong results: the released view compares by content, and the 'c' write goes through. On 3.15 both repros from the issue segfault. I checked that this patch applies cleanly there and fixes both.

I flipped one assert in test_gc_cleared_view. It was checking that a view cleared inside __buffer__ still compares equal, which is exactly the bug here.

Comment thread Objects/memoryobject.c Resolved
@alonfaraj
alonfaraj requested a review from vstinner November 13, 2026 11:57
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.

memoryview: a view released by __buffer__ or __index__ in the middle of == or item assignment is still used

2 participants