Repository navigation
Copy read-only image memory before frombytes and ImageCms writes - #10256
Open
tongwentao wants to merge 3 commits into
Open
tongwentao wants to merge 3 commits into
tongwentao wants to merge 3 commits into
Conversation
This branch has not been deployed
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.
Fixes #10235.
Implements @bigcat88's proposed copy-on-write fix from the issue.
Changes proposed in this pull request:
Image.frombytes()decodes into it. Check the core's flag, rather than calling_ensure_mutable(), because plugins can callfrombytes()from their ownload()while the image-level flag is still set.Validation (Linux, Python 3.11.15, Pillow built from this checkout, LittleCMS 2.14):
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.frombytes()and in-placeImageCmstransforms 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'sload()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.