Skip to content

Commit b336c34

Browse files
alicanalbayrakdon-vip
authored andcommitted
Fix ArrayIndexOutOfBoundsException in Extended XMP processing (drewnoakes#712)
Add bounds validation before System.arraycopy in processExtendedXMPChunk() to prevent crashes when processing malformed Extended XMP metadata. The fix handles two edge cases: - chunkOffset + copyLength exceeds buffer size - chunkOffset is negative (from uint32 > Integer.MAX_VALUE cast to int) Uses subtraction instead of addition to avoid integer overflow: if (chunkOffset < 0 || chunkOffset > buffer.length - copyLength) Fixes drewnoakes#308
1 parent 873adde commit b336c34

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

Source/com/drew/metadata/xmp/XmpReader.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,17 @@ private static byte[] processExtendedXMPChunk(@NotNull Metadata metadata, @NotNu
293293
extendedXMPBuffer = new byte[fullLength];
294294

295295
if (extendedXMPBuffer.length == fullLength) {
296-
System.arraycopy(segmentBytes, totalOffset, extendedXMPBuffer, chunkOffset, segmentLength - totalOffset);
296+
int copyLength = segmentLength - totalOffset;
297+
// Validate bounds before arraycopy to prevent ArrayIndexOutOfBoundsException.
298+
// Check for negative chunkOffset (from uint32 > Integer.MAX_VALUE) and use
299+
// subtraction instead of addition to avoid integer overflow.
300+
if (chunkOffset < 0 || chunkOffset > extendedXMPBuffer.length - copyLength) {
301+
XmpDirectory directory = new XmpDirectory();
302+
directory.addError(String.format("Extended XMP chunk would write beyond buffer bounds (offset=%d, length=%d, buffer size=%d)", chunkOffset, copyLength, extendedXMPBuffer.length));
303+
metadata.addDirectory(directory);
304+
} else {
305+
System.arraycopy(segmentBytes, totalOffset, extendedXMPBuffer, chunkOffset, copyLength);
306+
}
297307
} else {
298308
XmpDirectory directory = new XmpDirectory();
299309
directory.addError(String.format("Inconsistent length for the Extended XMP buffer: %d instead of %d", fullLength, extendedXMPBuffer.length));

0 commit comments

Comments
 (0)