diff options
author | Dominik Stadler <centic@apache.org> | 2023-08-07 09:42:17 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2023-08-07 09:42:17 +0000 |
commit | 31fd087a4895239e01d09ee68389b2a5e78c3d47 (patch) | |
tree | ddcf86ebc7e1fb14bad5b05cdc6a6c2c7036616f /poi-ooxml | |
parent | 6f054ddce1f84edc5f2f85f89904c03df3c1727a (diff) | |
download | poi-31fd087a4895239e01d09ee68389b2a5e78c3d47.tar.gz poi-31fd087a4895239e01d09ee68389b2a5e78c3d47.zip |
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=61249
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1911501 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r-- | poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java | 86 |
1 files changed, 45 insertions, 41 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 09d6a6eefe..1ddc55d98a 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -241,48 +241,52 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { for (RelationPart rp : getRelationParts()) { POIXMLDocumentPart p = rp.getDocumentPart(); String relation = rp.getRelationship().getRelationshipType(); - if (relation.equals(XWPFRelation.STYLES.getRelation())) { - this.styles = (XWPFStyles) p; - this.styles.onDocumentRead(); - } else if (relation.equals(XWPFRelation.THEME.getRelation())) { - this.theme = (XWPFTheme) p; - this.theme.onDocumentRead(); - } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) { - this.numbering = (XWPFNumbering) p; - this.numbering.onDocumentRead(); - } else if (relation.equals(XWPFRelation.FOOTER.getRelation())) { - XWPFFooter footer = (XWPFFooter) p; - footers.add(footer); - footer.onDocumentRead(); - } else if (relation.equals(XWPFRelation.HEADER.getRelation())) { - XWPFHeader header = (XWPFHeader) p; - headers.add(header); - header.onDocumentRead(); - } else if (relation.equals(XWPFRelation.COMMENT.getRelation())) { - this.comments = (XWPFComments) p; - this.comments.onDocumentRead(); - } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) { - settings = (XWPFSettings) p; - settings.onDocumentRead(); - } else if (relation.equals(XWPFRelation.IMAGES.getRelation())) { - XWPFPictureData picData = (XWPFPictureData) p; - picData.onDocumentRead(); - registerPackagePictureData(picData); - pictures.add(picData); - } else if (relation.equals(XWPFRelation.CHART.getRelation())) { - //now we can use all methods to modify charts in XWPFDocument - XWPFChart chartData = (XWPFChart) p; - charts.add(chartData); - } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) { - // We don't currently process the glossary itself - // Until we do, we do need to load the glossary child parts of it - for (POIXMLDocumentPart gp : p.getRelations()) { - // Trigger the onDocumentRead for all the child parts - // Otherwise we'll hit issues on Styles, Settings etc on save - // TODO: Refactor this to not need to access protected method - // from other package! Remove the static helper method once fixed!!! - POIXMLDocumentPart._invokeOnDocumentRead(gp); + try { + if (relation.equals(XWPFRelation.STYLES.getRelation())) { + this.styles = (XWPFStyles) p; + this.styles.onDocumentRead(); + } else if (relation.equals(XWPFRelation.THEME.getRelation())) { + this.theme = (XWPFTheme) p; + this.theme.onDocumentRead(); + } else if (relation.equals(XWPFRelation.NUMBERING.getRelation())) { + this.numbering = (XWPFNumbering) p; + this.numbering.onDocumentRead(); + } else if (relation.equals(XWPFRelation.FOOTER.getRelation())) { + XWPFFooter footer = (XWPFFooter) p; + footers.add(footer); + footer.onDocumentRead(); + } else if (relation.equals(XWPFRelation.HEADER.getRelation())) { + XWPFHeader header = (XWPFHeader) p; + headers.add(header); + header.onDocumentRead(); + } else if (relation.equals(XWPFRelation.COMMENT.getRelation())) { + this.comments = (XWPFComments) p; + this.comments.onDocumentRead(); + } else if (relation.equals(XWPFRelation.SETTINGS.getRelation())) { + settings = (XWPFSettings) p; + settings.onDocumentRead(); + } else if (relation.equals(XWPFRelation.IMAGES.getRelation())) { + XWPFPictureData picData = (XWPFPictureData) p; + picData.onDocumentRead(); + registerPackagePictureData(picData); + pictures.add(picData); + } else if (relation.equals(XWPFRelation.CHART.getRelation())) { + //now we can use all methods to modify charts in XWPFDocument + XWPFChart chartData = (XWPFChart) p; + charts.add(chartData); + } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) { + // We don't currently process the glossary itself + // Until we do, we do need to load the glossary child parts of it + for (POIXMLDocumentPart gp : p.getRelations()) { + // Trigger the onDocumentRead for all the child parts + // Otherwise we'll hit issues on Styles, Settings etc on save + // TODO: Refactor this to not need to access protected method + // from other package! Remove the static helper method once fixed!!! + POIXMLDocumentPart._invokeOnDocumentRead(gp); + } } + } catch (ClassCastException e) { + throw new IllegalArgumentException("Relation and type of document-part did not match, had relation " + relation + " and type of document-part: " + p.getClass()); } } initHyperlinks(); |