Skip to content

Commit 08a2b7c

Browse files
authored
Merge pull request #653 from drewnoakes/validate-jpeg-length
Validate lengths before parsing JPEG data
2 parents 6aee7b3 + c1d3320 commit 08a2b7c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Source/com/drew/metadata/jpeg/JpegReader.java

+14
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segm
7777
// The value of TAG_COMPRESSION_TYPE is determined by the segment type found
7878
directory.setInt(JpegDirectory.TAG_COMPRESSION_TYPE, segmentType.byteValue - JpegSegmentType.SOF0.byteValue);
7979

80+
final int JPEG_HEADER_SIZE = 1 + 2 + 2 + 1;
81+
82+
if (segmentBytes.length < JPEG_HEADER_SIZE) {
83+
directory.addError("Insufficient bytes for JPEG segment header.");
84+
return;
85+
}
86+
8087
SequentialReader reader = new SequentialByteArrayReader(segmentBytes);
8188

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

96+
final int JPEG_COMPONENT_SIZE = 1 + 1 + 1;
97+
98+
if (reader.available() < componentCount * JPEG_COMPONENT_SIZE) {
99+
directory.addError("Insufficient bytes for JPEG the requested number of JPEG components.");
100+
return;
101+
}
102+
89103
// for each component, there are three bytes of data:
90104
// 1 - Component ID: 1 = Y, 2 = Cb, 3 = Cr, 4 = I, 5 = Q
91105
// 2 - Sampling factors: bit 0-3 vertical, 4-7 horizontal

0 commit comments

Comments
 (0)