This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new a5f94f5cea Replace asserts on file data with real checks (#1557)
a5f94f5cea is described below
commit a5f94f5cea41ec9168c59499249e10ca4c5a4a1f
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Nov 9 10:32:15 2026 +0100
Replace asserts on file data with real checks (#1557)
These asserts only run with -ea. Without it, the code went on to throw
a NullPointerException or ArrayIndexOutOfBoundsException, or read data
at the wrong offset.
- EMF+: an unknown object type now throws a RecordFormatException. An
unknown brush type or pixel format is reported when the brush data
or the GDI bitmap is used, so parsing still works. An unknown image
or metafile type gives an unknown content or embedded type.
- EMF comments that read past their record throw a
RecordFormatException.
- WMF: an unknown raster operation is drawn like SRCCOPY, which also
covers images drawn before any raster operation is set. Partial
bitmap16 records skip their reserved bytes with skipFully.
- HSLF: EMF/WMF picture data shorter than its header throws a
RecordFormatException, getAutoNumberingStartAt returns null for a
paragraph without auto numbering, and adding the encryption record
to a deck without PersistPtrHolder/UserEditAtom throws a
CorruptPowerPointFileException.
- XSLF: table spans past the last column or row are logged and
limited to the table.
Part of #1556
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/poi/xslf/usermodel/XSLFTable.java | 9 +--
.../apache/poi/xslf/usermodel/TestXSLFTable.java | 14 +++++
.../apache/poi/hemf/record/emf/HemfComment.java | 5 +-
.../poi/hemf/record/emfplus/HemfPlusBrush.java | 5 +-
.../record/emfplus/HemfPlusGDIImageRenderer.java | 3 +
.../poi/hemf/record/emfplus/HemfPlusImage.java | 15 +++--
.../poi/hemf/record/emfplus/HemfPlusObject.java | 5 +-
.../poi/hemf/usermodel/HemfEmbeddedIterator.java | 13 +++--
.../main/java/org/apache/poi/hslf/blip/EMF.java | 6 +-
.../main/java/org/apache/poi/hslf/blip/WMF.java | 5 +-
.../poi/hslf/usermodel/HSLFSlideShowEncrypted.java | 5 +-
.../poi/hslf/usermodel/HSLFTextParagraph.java | 6 +-
.../org/apache/poi/hwmf/draw/HwmfGraphics.java | 7 ++-
.../org/apache/poi/hwmf/record/HwmfBitmap16.java | 6 +-
.../java/org/apache/poi/hwmf/record/HwmfFill.java | 3 +-
.../hemf/record/emfplus/TestHemfPlusObject.java | 66 ++++++++++++++++++++++
.../hslf/usermodel/TestHSLFSlideShowEncrypted.java | 36 ++++++++++++
.../java/org/apache/poi/hwmf/TestHwmfGraphics.java | 55 ++++++++++++++++++
18 files changed, 230 insertions(+), 34 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java
b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java
index 25ebae2aba..514910222a 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFTable.java
@@ -425,8 +425,10 @@ public class XSLFTable extends XSLFGraphicFrame implements
Iterable<XSLFTableRow
continue;
}
Rectangle2D mergedBounds = tc.getAnchor();
- for (int col2=col+1; col2<col+tc.getGridSpan(); col2++) {
- assert(col2 < cols);
+ if (col+tc.getGridSpan() > cols || row+tc.getRowSpan() > rows)
{
+ LOG.warn("table span past the table end - rendering result
is probably wrong");
+ }
+ for (int col2=col+1; col2<Math.min(col+tc.getGridSpan(),
cols); col2++) {
XSLFTableCell tc2 = getCell(row, col2);
if (tc2 == null) {
LOG.warn("unavailable table span - rendering result is
probably wrong");
@@ -437,8 +439,7 @@ public class XSLFTable extends XSLFGraphicFrame implements
Iterable<XSLFTableRow
}
mergedBounds.add(tc2.getAnchor());
}
- for (int row2=row+1; row2<row+tc.getRowSpan(); row2++) {
- assert(row2 < rows);
+ for (int row2=row+1; row2<Math.min(row+tc.getRowSpan(), rows);
row2++) {
XSLFTableCell tc2 = getCell(row2, col);
if (tc2 == null) {
LOG.warn("unavailable table span - rendering result is
probably wrong");
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTable.java
b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTable.java
index 128a852c97..5a87214b20 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTable.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFTable.java
@@ -568,4 +568,18 @@ class TestXSLFTable {
ppt.close();
}
+
+ @Test
+ void testSpanPastTableEnd() throws IOException {
+ try (XMLSlideShow ppt = new XMLSlideShow()) {
+ XSLFTable tab = ppt.createSlide().createTable(2, 2);
+ tab.setAnchor(new Rectangle2D.Double(0, 0, 200, 100));
+ // spans that reach past the last column and row of the table
+ tab.getCell(0, 1).setGridSpan(3);
+ tab.getCell(1, 0).setRowSpan(2);
+
+ assertDoesNotThrow(tab::updateCellAnchor);
+ assertNotNull(tab.getCell(0, 1).getAnchor());
+ }
+ }
}
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfComment.java
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfComment.java
index cf38dcf40f..ed16675bd9 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfComment.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfComment.java
@@ -273,7 +273,10 @@ public class HemfComment {
long readBytes = record.init(leis, dataSize);
final long skipBytes = recordSize-4-readBytes;
- assert (skipBytes >= 0);
+ if (skipBytes < 0) {
+ throw new RecordFormatException("Comment record limit
exceeded - readBytes: " + readBytes
+ + " / recordSize: " + recordSize);
+ }
leis.skipFully(skipBytes);
return record;
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusBrush.java
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusBrush.java
index 7cc4f28d91..c9bb99559f 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusBrush.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusBrush.java
@@ -350,8 +350,8 @@ public class HemfPlusBrush {
size = 0;
} else {
int brushInt = leis.readInt();
+ // an unknown type is only reported when the brush data is used
brushType = EmfPlusBrushType.valueOf(brushInt);
- assert(brushType != null);
size += LittleEndianConsts.INT_SIZE;
}
@@ -393,6 +393,9 @@ public class HemfPlusBrush {
* @throws IllegalStateException if the data cannot be processed
*/
public EmfPlusBrushData getBrushData(List<? extends EmfPlusObjectData>
continuedObjectData) {
+ if (brushType == null) {
+ throw new IllegalStateException("Unknown brush type");
+ }
EmfPlusBrushData brushData = brushType.constructor.get();
byte[] buf = getRawData(continuedObjectData);
try (UnsynchronizedByteArrayInputStream bis =
UnsynchronizedByteArrayInputStream.builder().setByteArray(buf).get()){
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusGDIImageRenderer.java
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusGDIImageRenderer.java
index 6c88fdd808..50c7bb3961 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusGDIImageRenderer.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusGDIImageRenderer.java
@@ -92,6 +92,9 @@ public class HemfPlusGDIImageRenderer extends
BitmapImageRenderer {
* @return the BufferedImage
*/
public BufferedImage readGDIImage(final byte[] data) {
+ if (pixelFormat == null) {
+ throw new IllegalStateException("Unknown pixel format");
+ }
int[] nBits, bOffs;
bOffs = switch (pixelFormat) {
case ARGB_32BPP -> {
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusImage.java
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusImage.java
index 4aa21cd62a..121fe87f68 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusImage.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusImage.java
@@ -366,7 +366,6 @@ public class HemfPlusImage {
pixelFormat = (bitmapType == EmfPlusBitmapDataType.PIXEL)
? EmfPlusPixelFormat.valueOf(pixelFormatInt)
: EmfPlusPixelFormat.UNDEFINED;
- assert (pixelFormat != null);
fileSize = dataSize - size;
@@ -511,6 +510,9 @@ public class HemfPlusImage {
private String getContentType(final byte[] data) {
PictureType pictureType = PictureType.UNKNOWN;
+ if (getImageDataType() == null) {
+ return pictureType.contentType;
+ }
switch (getImageDataType()) {
case BITMAP:
if (getBitmapType() == EmfPlusBitmapDataType.PIXEL) {
@@ -528,11 +530,12 @@ public class HemfPlusImage {
break;
case METAFILE:
- assert (getMetafileType() != null);
- pictureType = switch (getMetafileType()) {
- case Wmf, WmfPlaceable -> PictureType.WMF;
- case Emf, EmfPlusDual, EmfPlusOnly -> PictureType.EMF;
- };
+ if (getMetafileType() != null) {
+ pictureType = switch (getMetafileType()) {
+ case Wmf, WmfPlaceable -> PictureType.WMF;
+ case Emf, EmfPlusDual, EmfPlusOnly ->
PictureType.EMF;
+ };
+ }
break;
}
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusObject.java
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusObject.java
index e21bb226ff..9c90507182 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusObject.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusObject.java
@@ -44,6 +44,7 @@ import org.apache.poi.util.GenericRecordUtil;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndianConsts;
import org.apache.poi.util.LittleEndianInputStream;
+import org.apache.poi.util.RecordFormatException;
public class HemfPlusObject {
private static final int MAX_OBJECT_SIZE = 50_000_000;
@@ -172,7 +173,9 @@ public class HemfPlusObject {
objectId = getObjectId();
EmfPlusObjectType objectType = getObjectType();
- assert (objectType != null);
+ if (objectType == null) {
+ throw new RecordFormatException("Unknown EMF+ object type " +
OBJECT_TYPE.getValue(flags));
+ }
long size = 0;
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java
index 0997952077..dce21107dd 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/usermodel/HemfEmbeddedIterator.java
@@ -230,7 +230,6 @@ public class HemfEmbeddedIterator implements
Iterator<HwmfEmbedded> {
assert(epo.getObjectType() == EmfPlusObjectType.IMAGE);
EmfPlusImage img = epo.getObjectData();
- assert(img.getImageDataType() != null);
final HwmfEmbedded emb = getEmfPlusImageData();
if (emb == null) {
@@ -238,6 +237,10 @@ public class HemfEmbeddedIterator implements
Iterator<HwmfEmbedded> {
}
final HwmfEmbeddedType et;
+ if (img.getImageDataType() == null) {
+ emb.setEmbeddedType(HwmfEmbeddedType.UNKNOWN);
+ return emb;
+ }
switch (img.getImageDataType()) {
case BITMAP -> {
if (img.getBitmapType() == EmfPlusBitmapDataType.COMPRESSED) {
@@ -253,14 +256,13 @@ public class HemfEmbeddedIterator implements
Iterator<HwmfEmbedded> {
compressGDIBitmap(img, emb, et);
}
}
- case METAFILE -> {
- assert(img.getMetafileType() != null);
- et = switch (img.getMetafileType()) {
+ case METAFILE -> et = (img.getMetafileType() == null)
+ ? HwmfEmbeddedType.UNKNOWN
+ : switch (img.getMetafileType()) {
case Wmf, WmfPlaceable -> HwmfEmbeddedType.WMF;
case Emf, EmfPlusDual, EmfPlusOnly -> HwmfEmbeddedType.EMF;
default -> HwmfEmbeddedType.UNKNOWN;
};
- }
default -> et = HwmfEmbeddedType.UNKNOWN;
}
emb.setEmbeddedType(et);
@@ -304,7 +306,6 @@ public class HemfEmbeddedIterator implements
Iterator<HwmfEmbedded> {
boolean hasNext = false;
do {
EmfPlusImage img = epo.getObjectData();
- assert(img.getImageDataType() != null);
assert(!hasNext || img.getImageDataType() ==
HemfPlusImage.EmfPlusImageDataType.CONTINUED);
bos.write(img.getImageData());
current = null;
diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java
index c727c2dc79..28e273c087 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/EMF.java
@@ -67,8 +67,10 @@ public final class EMF extends Metafile {
InflaterInputStream inflater = new InflaterInputStream(is);
UnsynchronizedByteArrayOutputStream out =
UnsynchronizedByteArrayOutputStream.builder().get()
) {
- long len = IOUtils.skipFully(is,header.getSize() +
(long)CHECKSUM_SIZE);
- assert(len == header.getSize() + CHECKSUM_SIZE);
+ long skipLen = header.getSize() + (long)CHECKSUM_SIZE;
+ if (IOUtils.skipFully(is, skipLen) != skipLen) {
+ throw new RecordFormatException("EMF data is shorter than its
header");
+ }
final int maxLength = getMaxRecordLength();
long copied = IOUtils.copy(inflater, out, (long) maxLength + 1);
diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java
index df05410385..1a71ba1134 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/blip/WMF.java
@@ -64,8 +64,9 @@ public final class WMF extends Metafile {
Header header = new Header();
header.read(rawdata, Math.multiplyExact(CHECKSUM_SIZE,
getUIDInstanceCount()));
long skipLen = header.getSize() +
(long)CHECKSUM_SIZE*getUIDInstanceCount();
- long skipped = IOUtils.skipFully(is, skipLen);
- assert(skipped == skipLen);
+ if (IOUtils.skipFully(is, skipLen) != skipLen) {
+ throw new RecordFormatException("WMF data is shorter than its
header");
+ }
ImageHeaderWMF aldus = new ImageHeaderWMF(header.getBounds());
UnsynchronizedByteArrayOutputStream out =
UnsynchronizedByteArrayOutputStream.builder().get();
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java
index 277f9fc568..7dd121c7f6 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java
@@ -565,13 +565,16 @@ public class HSLFSlideShowEncrypted implements Closeable {
deaIdx = idx;
}
}
- assert(ueaIdx != -1 && ptrIdx != -1 && ptrIdx < ueaIdx);
if (deaIdx != -1) {
DocumentEncryptionAtom deaOld =
(DocumentEncryptionAtom)records[deaIdx];
dea.setLastOnDiskOffset(deaOld.getLastOnDiskOffset());
records[deaIdx] = dea;
return records;
} else {
+ if (ueaIdx == -1 || ptrIdx == -1 || ptrIdx > ueaIdx) {
+ throw new CorruptPowerPointFileException(
+ "Expected a PersistPtrHolder followed by a UserEditAtom to
add the encryption record");
+ }
PersistPtrHolder ptr = (PersistPtrHolder)records[ptrIdx];
UserEditAtom uea = ((UserEditAtom)records[ueaIdx]);
dea.setLastOnDiskOffset(ptr.getLastOnDiskOffset()-1);
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java
index a2f6bf4d7d..f2520ff284 100644
---
a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java
+++
b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java
@@ -500,12 +500,12 @@ public final class HSLFTextParagraph implements
TextParagraph<HSLFShape,HSLFText
}
TextPFException9[] ant = styleTextProp9Atom.getAutoNumberTypes();
int level = getIndentLevel();
- if (ant == null || level >= ant.length) {
+ if (ant == null || level < 0 || level >= ant.length) {
return null;
}
+ // null, if the paragraph has no auto numbered bullet
Short startAt = ant[level].getAutoNumberStartNumber();
- assert(startAt != null);
- return startAt.intValue();
+ return (startAt == null) ? null : startAt.intValue();
}
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfGraphics.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfGraphics.java
index 68e484b874..fedc1328af 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfGraphics.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/draw/HwmfGraphics.java
@@ -68,6 +68,7 @@ import org.apache.poi.hwmf.record.HwmfObjectTableEntry;
import org.apache.poi.hwmf.record.HwmfPenStyle;
import org.apache.poi.hwmf.record.HwmfPenStyle.HwmfLineDash;
import org.apache.poi.hwmf.record.HwmfRegionMode;
+import org.apache.poi.hwmf.record.HwmfTernaryRasterOp;
import org.apache.poi.hwmf.record.HwmfText.WmfExtTextOutOptions;
import org.apache.poi.hwmf.usermodel.HwmfCharsetAware;
import org.apache.poi.sl.draw.BitmapImageRenderer;
@@ -732,13 +733,15 @@ public class HwmfGraphics implements HwmfCharsetAware {
return;
}
HwmfDrawProperties prop = getProperties();
+ // an unset or unknown raster op is drawn like SRCCOPY
+ final HwmfTernaryRasterOp rasterOp = (prop.getRasterOp3() == null) ?
HwmfTernaryRasterOp.SRCCOPY : prop.getRasterOp3();
// handle raster op
// currently the raster op as described in
https://docs.microsoft.com/en-us/windows/desktop/gdi/ternary-raster-operations
// are not supported, as we would need to extract the destination
image area from the underlying buffered image
// and therefore would make it mandatory that the graphics context
must be from a buffered image
// furthermore I doubt the purpose of bitwise image operations on
non-black/white images
- switch (prop.getRasterOp3()) {
+ switch (rasterOp) {
case D:
// keep destination, i.e. do nothing
break;
@@ -787,7 +790,7 @@ public class HwmfGraphics implements HwmfCharsetAware {
// of the referenced image and can be also negative
Composite old = graphicsCtx.getComposite();
int newComp;
- switch (prop.getRasterOp3()) {
+ switch (rasterOp) {
default:
case SRCCOPY:
newComp = AlphaComposite.SRC_OVER;
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmap16.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmap16.java
index cb8315d8ce..555eaf324a 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmap16.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmap16.java
@@ -71,11 +71,9 @@ public class HwmfBitmap16 implements GenericRecord {
int size =
2*LittleEndianConsts.BYTE_SIZE+4*LittleEndianConsts.SHORT_SIZE;
if (isPartial) {
// Bits (4 bytes): This field MUST be ignored.
- long skipSize = leis.skip(LittleEndianConsts.INT_SIZE);
- assert(skipSize == LittleEndianConsts.INT_SIZE);
+ leis.skipFully(LittleEndianConsts.INT_SIZE);
// Reserved (18 bytes): This field MUST be ignored.
- skipSize = leis.skip(18);
- assert(skipSize == 18);
+ leis.skipFully(18);
size += 18+LittleEndianConsts.INT_SIZE;
}
diff --git
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfFill.java
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfFill.java
index 5bd7db8028..c93bb13fde 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfFill.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfFill.java
@@ -984,8 +984,9 @@ public class HwmfFill {
int rasterOpCode = leis.readUShort();
int rasterOpIndex = leis.readUShort();
+ // null for an unknown raster operation, which is drawn like SRCCOPY
HwmfTernaryRasterOp rasterOperation =
HwmfTernaryRasterOp.valueOf(rasterOpIndex);
- assert(rasterOperation != null && rasterOpCode ==
rasterOperation.getOpCode());
+ assert(rasterOperation == null || rasterOpCode ==
rasterOperation.getOpCode());
return rasterOperation;
}
}
diff --git
a/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emfplus/TestHemfPlusObject.java
b/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emfplus/TestHemfPlusObject.java
new file mode 100644
index 0000000000..dc3d6d2b67
--- /dev/null
+++
b/poi-scratchpad/src/test/java/org/apache/poi/hemf/record/emfplus/TestHemfPlusObject.java
@@ -0,0 +1,66 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hemf.record.emfplus;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Collections;
+
+import org.apache.poi.hemf.record.emfplus.HemfPlusBrush.EmfPlusBrush;
+import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObject;
+import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObjectType;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInputStream;
+import org.apache.poi.util.RecordFormatException;
+import org.junit.jupiter.api.Test;
+
+class TestHemfPlusObject {
+
+ @Test
+ void rejectsUnknownObjectType() throws IOException {
+ EmfPlusObject obj = new EmfPlusObject();
+ try (LittleEndianInputStream leis = new LittleEndianInputStream(new
ByteArrayInputStream(new byte[0]))) {
+ // object type 0x7F isn't defined
+ RecordFormatException ex =
assertThrows(RecordFormatException.class,
+ () -> obj.init(leis, 0, HemfPlusRecordType.object.id, 0x7F00));
+ assertEquals("Unknown EMF+ object type 127", ex.getMessage());
+ }
+ }
+
+ @Test
+ void unknownBrushTypeFailsWhenUsed() throws IOException {
+ byte[] data = new byte[8];
+ // EmfPlusGraphicsVersion: metafile signature 0xDBC01, graphics
version 1
+ LittleEndian.putInt(data, 0, 0xDBC01001);
+ LittleEndian.putInt(data, 4, 0x7FFF);
+
+ EmfPlusBrush brush = new EmfPlusBrush();
+ try (LittleEndianInputStream leis = new LittleEndianInputStream(new
ByteArrayInputStream(data))) {
+ // parsing keeps working, so the rest of the picture can still be
read
+ brush.init(leis, data.length, EmfPlusObjectType.BRUSH, 0);
+ }
+ assertNull(brush.getGenericRecordType());
+ IllegalStateException ex = assertThrows(IllegalStateException.class,
+ () -> brush.getBrushData(Collections.emptyList()));
+ assertEquals("Unknown brush type", ex.getMessage());
+ }
+}
diff --git
a/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestHSLFSlideShowEncrypted.java
b/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestHSLFSlideShowEncrypted.java
new file mode 100644
index 0000000000..55ffb2ea06
--- /dev/null
+++
b/poi-scratchpad/src/test/java/org/apache/poi/hslf/usermodel/TestHSLFSlideShowEncrypted.java
@@ -0,0 +1,36 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hslf.usermodel;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.apache.poi.hslf.exceptions.CorruptPowerPointFileException;
+import org.apache.poi.hslf.record.DocumentEncryptionAtom;
+import org.apache.poi.hslf.record.Record;
+import org.junit.jupiter.api.Test;
+
+class TestHSLFSlideShowEncrypted {
+
+ @Test
+ void addEncryptionRecordNeedsPersistRecords() {
+ // without a PersistPtrHolder and UserEditAtom there is nowhere to add
the encryption record
+ Record[] records = new Record[0];
+ assertThrows(CorruptPowerPointFileException.class,
+ () -> HSLFSlideShowEncrypted.addEncryptionRecord(records, new
DocumentEncryptionAtom()));
+ }
+}
diff --git
a/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfGraphics.java
b/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfGraphics.java
new file mode 100644
index 0000000000..84ed1867f2
--- /dev/null
+++ b/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfGraphics.java
@@ -0,0 +1,55 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hwmf;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+
+import org.apache.poi.hwmf.draw.HwmfGraphics;
+import org.junit.jupiter.api.Test;
+
+class TestHwmfGraphics {
+
+ /**
+ * An unset or unknown raster operation is drawn like SRCCOPY
+ */
+ @Test
+ void drawImageWithoutRasterOp() {
+ BufferedImage canvas = new BufferedImage(10, 10,
BufferedImage.TYPE_INT_ARGB);
+ Graphics2D g = canvas.createGraphics();
+ try {
+ Rectangle2D bounds = new Rectangle2D.Double(0, 0, 10, 10);
+ HwmfGraphics hg = new HwmfGraphics(g, bounds);
+
+ BufferedImage img = new BufferedImage(2, 2,
BufferedImage.TYPE_INT_ARGB);
+ Graphics2D ig = img.createGraphics();
+ ig.setColor(Color.RED);
+ ig.fillRect(0, 0, 2, 2);
+ ig.dispose();
+
+ hg.drawImage(img, new Rectangle2D.Double(0, 0, 2, 2), bounds);
+ } finally {
+ g.dispose();
+ }
+ assertEquals(Color.RED.getRGB(), canvas.getRGB(5, 5));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]