Bug 66425: Avoid a ClassCastException found via oss-fuzz

We try to avoid throwing ClassCastException, but it was possible
to trigger one here with a specially crafted input-file

Should fix https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=61330

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1911586 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2023-08-10 06:14:44 +00:00
parent 316738c9d0
commit 80264d5648
3 changed files with 31 additions and 25 deletions

View File

@ -181,32 +181,38 @@ public class AttachmentChunks implements ChunkGroup {
// - ATTACH_LONG_PATHNAME
// - ATTACH_SIZE
final int chunkId = chunk.getChunkId();
if (chunkId == ATTACH_DATA.id) {
if (chunk instanceof ByteChunk) {
attachData = (ByteChunk) chunk;
} else if (chunk instanceof DirectoryChunk) {
attachmentDirectory = (DirectoryChunk) chunk;
} else {
LOG.atError().log("Unexpected data chunk of type {}", chunk.getEntryName());
}
} else if (chunkId == ATTACH_EXTENSION.id) {
attachExtension = (StringChunk) chunk;
} else if (chunkId == ATTACH_FILENAME.id) {
attachFileName = (StringChunk) chunk;
} else if (chunkId == ATTACH_LONG_FILENAME.id) {
attachLongFileName = (StringChunk) chunk;
} else if (chunkId == ATTACH_MIME_TAG.id) {
attachMimeTag = (StringChunk) chunk;
} else if (chunkId == ATTACH_RENDERING.id) {
attachRenderingWMF = (ByteChunk) chunk;
} else if (chunkId == ATTACH_CONTENT_ID.id) {
attachContentId = (StringChunk) chunk;
} else {
LOG.atWarn().log("Currently unsupported attachment chunk property will be ignored. {}", chunk.getEntryName());
}
// And add to the main list
allChunks.add(chunk);
try {
if (chunkId == ATTACH_DATA.id) {
if (chunk instanceof ByteChunk) {
attachData = (ByteChunk) chunk;
} else if (chunk instanceof DirectoryChunk) {
attachmentDirectory = (DirectoryChunk) chunk;
} else {
LOG.atError().log("Unexpected data chunk of type {}", chunk.getEntryName());
}
} else if (chunkId == ATTACH_EXTENSION.id) {
attachExtension = (StringChunk) chunk;
} else if (chunkId == ATTACH_FILENAME.id) {
attachFileName = (StringChunk) chunk;
} else if (chunkId == ATTACH_LONG_FILENAME.id) {
attachLongFileName = (StringChunk) chunk;
} else if (chunkId == ATTACH_MIME_TAG.id) {
attachMimeTag = (StringChunk) chunk;
} else if (chunkId == ATTACH_RENDERING.id) {
attachRenderingWMF = (ByteChunk) chunk;
} else if (chunkId == ATTACH_CONTENT_ID.id) {
attachContentId = (StringChunk) chunk;
} else {
LOG.atWarn().log("Currently unsupported attachment chunk property will be ignored. {}", chunk.getEntryName());
}
// And add to the main list
allChunks.add(chunk);
} catch (ClassCastException e) {
throw new IllegalArgumentException("ChunkId and type of chunk did not match, had id " +
chunkId + " and type of chunk: " + chunk.getClass(), e);
}
}
/**

Binary file not shown.