Fix CCITTFactory.readlong#484
Open
valerybokov wants to merge 2 commits into
Open
Conversation
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.
Summary
CCITTFactory.readlong()reads a TIFFLONGfield (an unsigned 32-bit value per the TIFF spec) by assembling four bytes with anint, then returning thatint. At the call site (long address = readlong(...)), theintis widened tolong, which in Java sign-extends. If the top bit of the 32-bit value is set (i.e. the raw value is>= 0x80000000), the widenedlongbecomes a large negative number instead of the correct unsigned value — corrupting IFD offsets used directly inreader.seek(address).In practice this is latent rather than commonly hit: classic TIFF (non-BigTIFF) is capped near 4GB by its own 32-bit offset fields, and CCITT G3/G4-compressed files in particular are typically KB-to-low-MB in size, so authentic offsets essentially never cross that boundary. The realistic risk is a malformed/corrupted/adversarial TIFF whose 4-byte offset or count field has the high bit set —
readlongtrusts those bytes without validating against the actual file size, so such input silently produces a corrupted negative seek target rather than a controlled error.Fix
readlongmethodreadlongnow returnslongand masks the assembled value with& 0xFFFFFFFFL, so it's always treated as unsigned when read.address(already declaredlong) now receives the correct unsigned value automatically.countandval, which are intentionally kept asint(small tag values used withparams.setInt(...)/ buffer sizes), now narrow explicitly via(int)casts — same bit pattern as before, so their behavior is unchanged.Test plan
Added
CCITTFactoryTest#testReadLongIsUnsigned, which invokes the privatereadlongvia reflection against crafted byte sequences with the high bit set (0xFFFFFFFFand0x80000000, in both byte orders) and asserts the returnedlongis the correct non-negative unsigned value.int-returning implementation (e.g. expected4294967295, got-1) and passes against the fix.CCITTFactoryTestsuite (small real-world CCITT G3/G4 fixtures) continues to pass — those files never exercised this path since their offsets are well under0x80000000.