Skip to content

Add enforce to check for integer overflow#3514

Merged
kevinbackhouse merged 2 commits intoExiv2:mainfrom
kevinbackhouse:fix-issue-3513
Feb 27, 2026
Merged

Add enforce to check for integer overflow#3514
kevinbackhouse merged 2 commits intoExiv2:mainfrom
kevinbackhouse:fix-issue-3513

Conversation

@kevinbackhouse
Copy link
Copy Markdown
Collaborator

fixes: #3513

@kevinbackhouse
Copy link
Copy Markdown
Collaborator Author

@mergify backport 0.28.x

@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 26, 2026

backport 0.28.x

✅ Backports have been created

Details

Cherry-pick of 7adedce has failed:

On branch mergify/bp/0.28.x/pr-3514
Your branch is up to date with 'origin/0.28.x'.

You are currently cherry-picking commit 7adedce8.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	new file:   test/data/issue_3513_poc.psd
	new file:   tests/bugfixes/github/test_issue_3513.py

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   tests/regression_tests/test_regression_allfiles.py

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@kevinbackhouse kevinbackhouse marked this pull request as ready for review February 26, 2026 21:26
Copilot AI review requested due to automatic review settings February 26, 2026 21:26
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a regression test and a defensive enforce check to prevent integer overflow while parsing PSD resource blocks (issue #3513).

Changes:

  • Added a new system test for issue #3513 expecting kerCorruptedMetadata.
  • Added the PoC PSD file to the regression “all files” list.
  • Added an Internal::enforce bound check before casting/using preview size in PsdImage::readResourceBlock.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/regression_tests/test_regression_allfiles.py Includes the issue #3513 PSD PoC in the regression corpus.
tests/bugfixes/github/test_issue_3513.py Adds a targeted system test asserting the expected failure mode.
src/psdimage.cpp Adds an overflow guard when handling native preview resource sizes; minor comment formatting cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -126,6 +126,7 @@ def get_valid_files(data_dir):
"pocIssue283.jpg",
"poc_1522.jp2",
"xmpsdk.xmp",
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_valid_files now includes issue_3513_poc.psd, but the added dedicated test expects this file to fail with kerCorruptedMetadata. This mismatch makes the intent of the “valid files” list harder to understand. Consider either (a) moving this PoC to a separate list for negative/invalid inputs, or (b) adding an inline comment clarifying that this file is intentionally malformed but included for non-crash coverage (if that’s the purpose of this list).

Suggested change
"xmpsdk.xmp",
"xmpsdk.xmp",
# Intentionally malformed PoC covered by a dedicated test that expects
# kerCorruptedMetadata; keep it out of the generic "valid files" run.

Copilot uses AI. Check for mistakes.
import system_tests


class test_issue_3513_PsdImage_readResourceBlock(metaclass=system_tests.CaseMeta):
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test class name mixes conventions (lowercase test_... prefix plus PsdImage_readResourceBlock). For readability and consistency with common Python conventions, consider using CapWords for the class name (while keeping the file and/or test discovery patterns intact if required by system_tests).

Suggested change
class test_issue_3513_PsdImage_readResourceBlock(metaclass=system_tests.CaseMeta):
class TestIssue3513PsdImageReadResourceBlock(metaclass=system_tests.CaseMeta):

Copilot uses AI. Check for mistakes.
Comment on lines +287 to 292
Internal::enforce(nativePreview.size_ <= static_cast<size_t>(std::numeric_limits<long>::max()),
Exiv2::ErrorCode::kerCorruptedMetadata);

if (nativePreview.size_ > 0 && nativePreview.position_ > 0) {
io_->seek(static_cast<long>(nativePreview.size_), BasicIo::cur);
if (io_->error() || io_->eof())
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new enforce protects the static_cast<long>(nativePreview.size_) call, but similar overflow/invalid-range risks can exist for other offset-like fields (e.g., nativePreview.position_) and for any arithmetic combining them (such as position + size). If position_ is later used for seek/indexing in this function, consider adding corresponding bounds checks (and overflow-safe addition checks where applicable) using the same error code to fully address integer-overflow vectors in this parsing path.

Copilot uses AI. Check for mistakes.
neheb
neheb previously approved these changes Feb 26, 2026
Copy link
Copy Markdown
Collaborator

@kmilos kmilos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weirdly enough, clang-format is not happy w/ the comment indent change...

@mergify mergify bot dismissed neheb’s stale review February 27, 2026 10:39

Pull request has been modified.

@kevinbackhouse
Copy link
Copy Markdown
Collaborator Author

Weirdly enough, clang-format is not happy w/ the comment indent change...

I don't understand what happened there. That change was because I ran clang-format before committing. Maybe it's caused by a version mismatch: I'm using version 20.1.2.

Regardless, I've reverted that change so hopefully the tests will pass now.

@kevinbackhouse kevinbackhouse merged commit 659db31 into Exiv2:main Feb 27, 2026
99 of 105 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

found by fuzz-preview: Uncaught exception: cannot create std::vector larger than max_size()

4 participants