Skip to content

Copy read-only image memory before frombytes and ImageCms writes - #10256

Open
tongwentao wants to merge 3 commits into
python-pillow:mainfrom
tongwentao:fix/readonly-image-writes
Open

tongwentao wants to merge 3 commits into
python-pillow:mainfrom
tongwentao:fix/readonly-image-writes

Conversation

@tongwentao

@tongwentao tongwentao commented Oct 20, 2026 •

Copy link
Copy Markdown

Fixes #10235.

Implements @bigcat88's proposed copy-on-write fix from the issue.

Changes proposed in this pull request:

  • Copy a read-only imaging core before Image.frombytes() decodes into it. Check the core's flag, rather than calling _ensure_mutable(), because plugins can call frombytes() from their own load() while the image-level flag is still set.
  • Ensure an existing ImageCms output is mutable before applying the transform, including the in-place path.
  • Add regressions for bytes-backed L/RGBA images, explicit and in-place ImageCms outputs, and a mutable core whose image-level flag is still set. Add a 13.1.0 release note.

Validation (Linux, Python 3.11.15, Pillow built from this checkout, LittleCMS 2.14):

  • Before the source fix: 4 failed, 1 passed for the new regression/control cases. The failures show the shared bytes being changed or the output remaining read-only.
  • python3 selftest.py: 59 tests passed.
  • python3 -m pytest Tests/test_image_frombytes.py Tests/test_imagecms.py Tests/test_file_gbr.py Tests/test_file_wal.py -q: 53 passed.
  • python3 -m mypy src/PIL/Image.py src/PIL/ImageCms.py Tests/test_image_frombytes.py Tests/test_imagecms.py: passed with mypy 2.3.1.
  • pre-commit run --all-files: all applicable hooks passed, including Ruff, Black, Bandit and Sphinx Lint. The checkout omits unrelated binary test fixtures.
  • Re-ran both memory-mapped TIFF examples from frombytes() and in-place ImageCms transforms write into read-only image memory #10235 with generated files: both complete and the original file bytes remain unchanged.

The complete repository test suite and Windows/macOS were not run locally.

Codex helped implement the reporter's fix, write the regression tests, and run these checks. The tests were run against the original code before applying the source change, and the final diff was inspected for plugin-loading and copy-on-write behavior.

Upstream synchronization

Merged current main and resolved the conflict in the 13.1.0 release notes by retaining both the read-only image fix and the upstream resize entry. The functional changes in this PR remain the same.

Rebuilt the editable C extensions, then verified selftest.py (59 passed), the image/frombytes/ImageCms tests (230 passed, 1 skipped), and the changed-file pre-commit checks. The broader image test required upstream image fixtures omitted by the local sparse checkout; those fixtures were supplied in a temporary test directory, preserving the checkout configuration.

Behavior and review notes

The relevant invariant is that a write changes the image's writable copy without changing the bytes or memory-mapped storage it originally shared. The frombytes() path checks the imaging core's read-only state to avoid re-entering a plugin's load() through _ensure_mutable(). ImageCms still uses the normal mutability helper for existing output images. The mutable-core control protects the plugin-loading case as well as the read-only cases.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

frombytes() and in-place ImageCms transforms write into read-only image memory

2 participants