Skip to content

gh-161081: Reject mismatched non-contiguous buffers in PyObject_CopyData - #161514

Open
raman118 wants to merge 1 commit into
python:mainfrom
raman118:gh-161081-copydata-shape
Open

raman118 wants to merge 1 commit into
python:mainfrom
raman118:gh-161081-copydata-shape

Conversation

@raman118

@raman118 raman118 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Fixes gh-161081.

PyObject_CopyData() used source-buffer indices for both buffers on its non-contiguous copy path. When their shapes differed, this could write to incorrect offsets or read beyond the source index array. Validate that non-contiguous source and destination buffers have the same dimensions and shape before copying.

Add a regression test using _testbuffer arrays with different shapes. The test failed before this change because the copy returned success; it now raises BufferError.

Tests:

  • PCbuild\\build.bat -d -p x64 -M -q
  • PCbuild\\amd64\\python_d.exe -m test -v -m test_mismatched_shape test_buffer
  • PCbuild\\amd64\\python_d.exe -m test -v test_buffer (98 passed, 2 skipped)
  • PCbuild\\amd64\\python_d.exe Tools\\patchcheck\\patchcheck.py

AI assistance was used to investigate the issue and prepare this change. I reviewed the code and test behavior, including the failing pre-fix regression, before opening this PR.

@python-cla-bot

python-cla-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@raman118
raman118 force-pushed the gh-161081-copydata-shape branch from affb1fe to f141e72 Compare November 12, 2026 21:29
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.

PyObject_CopyData() copies wrong data, and can corrupt memory, when source and destination shapes differ and the source is not contiguous

1 participant