Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate lengths before parsing JPEG data #653

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Source/com/drew/metadata/jpeg/JpegReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segm
// The value of TAG_COMPRESSION_TYPE is determined by the segment type found
directory.setInt(JpegDirectory.TAG_COMPRESSION_TYPE, segmentType.byteValue - JpegSegmentType.SOF0.byteValue);

final int JPEG_HEADER_SIZE = 1 + 2 + 2 + 1;

if (segmentBytes.length < JPEG_HEADER_SIZE) {
directory.addError("Insufficient bytes for JPEG segment header.");
return;
}

SequentialReader reader = new SequentialByteArrayReader(segmentBytes);

try {
Expand All @@ -86,6 +93,13 @@ public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segm
short componentCount = reader.getUInt8();
directory.setInt(JpegDirectory.TAG_NUMBER_OF_COMPONENTS, componentCount);

final int JPEG_COMPONENT_SIZE = 1 + 1 + 1;

if (reader.available() < componentCount * JPEG_COMPONENT_SIZE) {
directory.addError("Insufficient bytes for JPEG the requested number of JPEG components.");
return;
}

// for each component, there are three bytes of data:
// 1 - Component ID: 1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q
// 2 - Sampling factors: bit 0-3 vertical, 4-7 horizontal
Expand Down
Loading