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 4df7edafae Log EMF/WMF spec violations instead of asserting them 
(#1559)
4df7edafae is described below

commit 4df7edafae0b2c450f7f833020f97b3c3cdd2258
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Nov 9 13:19:33 2026 +0100

    Log EMF/WMF spec violations instead of asserting them (#1559)
    
    * Log EMF/WMF spec violations instead of asserting them
    
    These asserts checked values the spec says MUST or SHOULD have, but
    parsing carries on without them. With -ea they rejected files that
    are otherwise readable. They now log a warning, like the existing
    checks in HemfFill and HemfPlusMisc.
    
    Three of the asserts hid real problems:
    - an EMF+ image of unknown type read 8 bytes past its record, as its
      data size included the version and type already read
    - an EMF+ record reading past its size failed on a negative skip, it
      now throws a RecordFormatException like HemfRecordIterator
    - the image size check for a BitmapCoreHeader compared against a
      field that header doesn't have, so it could never pass. Such
      headers also had no compression set, which threw a
      NullPointerException for bitmaps of more than 8 bits per pixel.
    
    Part of #1556
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * HWMF: reject an unknown bitmap compression with an IOException
    
    Compression.valueOf returns null for an unknown value, and readColors
    switched on it, which threw a NullPointerException for bitmaps of
    more than 8 bits per pixel. It now throws the IOException it already
    uses for compressions that don't fit the bit count.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../apache/poi/hemf/record/emf/HemfComment.java    |  8 ++-
 .../org/apache/poi/hemf/record/emf/HemfDraw.java   |  8 ++-
 .../org/apache/poi/hemf/record/emf/HemfMisc.java   |  8 ++-
 .../apache/poi/hemf/record/emf/HemfPalette.java    |  8 ++-
 .../poi/hemf/record/emfplus/HemfPlusDraw.java      | 25 ++++++--
 .../poi/hemf/record/emfplus/HemfPlusImage.java     |  5 +-
 .../record/emfplus/HemfPlusRecordIterator.java     |  4 +-
 .../poi/hemf/usermodel/HemfEmbeddedIterator.java   |  8 ++-
 .../org/apache/poi/hwmf/record/HwmfBitmapDib.java  | 13 ++++-
 .../java/org/apache/poi/hwmf/record/HwmfFill.java  |  8 ++-
 .../hemf/record/emfplus/TestHemfPlusObject.java    | 48 ++++++++++++++++
 .../org/apache/poi/hwmf/TestHwmfBitmapDib.java     | 67 ++++++++++++++++++++++
 12 files changed, 191 insertions(+), 19 deletions(-)

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 ed16675bd9..52287cbb04 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
@@ -182,7 +182,9 @@ public class HemfComment {
             if (commentIdentifier == HemfCommentRecordType.emfPublic.id) {
                 commentIdentifier = (int) leis.readUInt();
             }
-            assert(commentIdentifier == commentType.id);
+            if (commentIdentifier != commentType.id) {
+                LOG.atWarn().log("Unexpected comment identifier for {} in emf 
file: {}", commentType, commentIdentifier);
+            }
         }
 
         @Override
@@ -237,7 +239,9 @@ public class HemfComment {
                 // as a comment record. This value MUST be 0x00000046.
                 try {
                     long type = leis.readUInt();
-                    assert(type == HemfRecordType.comment.id);
+                    if (type != HemfRecordType.comment.id) {
+                        LOG.atWarn().log("Unexpected record type in emf 
comment: {}", type);
+                    }
                 } catch (RuntimeException e) {
                     // EOF
                     return null;
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfDraw.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfDraw.java
index 6b8ed94c57..75322ceaf3 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfDraw.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfDraw.java
@@ -32,12 +32,14 @@ import java.util.Map;
 import java.util.function.Supplier;
 import java.util.stream.IntStream;
 
+import org.apache.logging.log4j.Logger;
 import org.apache.poi.hemf.draw.HemfDrawProperties;
 import org.apache.poi.hemf.draw.HemfGraphics;
 import org.apache.poi.hemf.usermodel.HemfPicture;
 import org.apache.poi.hwmf.draw.HwmfGraphics.FillDrawStyle;
 import org.apache.poi.hwmf.record.HwmfDraw;
 import org.apache.poi.hwmf.record.HwmfDraw.WmfSelectObject;
+import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.util.ArrayUtil;
 import org.apache.poi.util.GenericRecordJsonWriter;
 import org.apache.poi.util.GenericRecordUtil;
@@ -46,6 +48,8 @@ import org.apache.poi.util.LittleEndianConsts;
 import org.apache.poi.util.LittleEndianInputStream;
 
 public final class HemfDraw {
+    private static final Logger LOG = PoiLogManager.getLogger(HemfDraw.class);
+
     // arbitrary limit to avoid OOM on malformed files. This may need 
increasing if "normal" files have more than this
     private static final int DEFAULT_MAX_NUMBER_OF_POLYGONS = 100_000;
     private static int MAX_NUMBER_OF_POLYGONS = DEFAULT_MAX_NUMBER_OF_POLYGONS;
@@ -994,7 +998,9 @@ public final class HemfDraw {
                     case 0x04:
                         int mode2 = leis.readUByte();
                         int mode3 = leis.readUByte();
-                        assert(mode2 == 0x04 && (mode3 == 0x04 || mode3 == 
0x05));
+                        if (mode2 != 0x04 || (mode3 != 0x04 && mode3 != 0x05)) 
{
+                            LOG.atWarn().log("Unexpected point types after 
PT_BEZIERTO in emf file: {}, {}", mode2, mode3);
+                        }
                         if ((i + 2) >= points.length) {
                             throw new IllegalStateException("Points index 
causes index out of bounds");
                         }
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
index 6698971c39..e3597368e5 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfMisc.java
@@ -30,6 +30,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.function.Supplier;
 
+import org.apache.logging.log4j.Logger;
 import org.apache.poi.hemf.draw.HemfDrawProperties;
 import org.apache.poi.hemf.draw.HemfGraphics;
 import org.apache.poi.hemf.usermodel.HemfPicture;
@@ -48,6 +49,7 @@ import org.apache.poi.hwmf.record.HwmfObjectTableEntry;
 import org.apache.poi.hwmf.record.HwmfPalette.PaletteEntry;
 import org.apache.poi.hwmf.record.HwmfPenStyle;
 import org.apache.poi.hwmf.record.HwmfPenStyle.HwmfLineDash;
+import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.util.GenericRecordJsonWriter;
 import org.apache.poi.util.GenericRecordUtil;
 import org.apache.poi.util.LittleEndianConsts;
@@ -55,6 +57,8 @@ import org.apache.poi.util.LittleEndianInputStream;
 
 @SuppressWarnings("WeakerAccess")
 public class HemfMisc {
+    private static final Logger LOG = PoiLogManager.getLogger(HemfMisc.class);
+
 
     public enum HemfModifyWorldTransformMode {
         /**
@@ -625,7 +629,9 @@ public class HemfMisc {
             final int numStyleEntries = (int) leis.readUInt();
             size += LittleEndianConsts.INT_SIZE;
 
-            assert (numStyleEntries == 0 || penStyle.getLineDash() == 
HwmfLineDash.USERSTYLE);
+            if (numStyleEntries != 0 && penStyle.getLineDash() != 
HwmfLineDash.USERSTYLE) {
+                LOG.atWarn().log("Unexpected style entries for a pen without 
user style in emf file: {}", numStyleEntries);
+            }
 
             // An optional array of 32-bit unsigned integers that defines the 
lengths of
             // dashes and gaps in the line drawn by this pen, when the value 
of PenStyle is
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfPalette.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfPalette.java
index e1e24a475a..63a3f8bc5c 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfPalette.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emf/HemfPalette.java
@@ -21,14 +21,18 @@ import java.io.IOException;
 import java.util.Map;
 import java.util.function.Supplier;
 
+import org.apache.logging.log4j.Logger;
 import org.apache.poi.hemf.draw.HemfGraphics;
 import org.apache.poi.hwmf.record.HwmfPalette;
+import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.util.GenericRecordUtil;
 import org.apache.poi.util.LittleEndianConsts;
 import org.apache.poi.util.LittleEndianInputStream;
 
 @SuppressWarnings("WeakerAccess")
 public class HemfPalette {
+    private static final Logger LOG = 
PoiLogManager.getLogger(HemfPalette.class);
+
     /** The EMR_SELECTPALETTE record specifies a logical palette for the 
playback device context. */
     public static class EmfSelectPalette extends HwmfPalette.WmfSelectPalette 
implements HemfRecord {
         @Override
@@ -74,7 +78,9 @@ public class HemfPalette {
             paletteIndex = (int) leis.readUInt();
             /* A 16-bit unsigned integer that specifies the version number of 
the system. This MUST be 0x0300. */
             int version = leis.readUShort();
-            assert(version == 0x0300);
+            if (version != 0x0300) {
+                LOG.atWarn().log("Unexpected palette version in emf file: {}", 
version);
+            }
             long size = readPaletteEntries(leis, -1);
             return size + LittleEndianConsts.INT_SIZE + 
LittleEndianConsts.SHORT_SIZE;
         }
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusDraw.java
 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusDraw.java
index 38fdc8eca0..bcacec3a1c 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusDraw.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusDraw.java
@@ -41,6 +41,7 @@ import java.util.function.Supplier;
 import org.apache.commons.math3.linear.LUDecomposition;
 import org.apache.commons.math3.linear.MatrixUtils;
 import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.logging.log4j.Logger;
 import org.apache.poi.hemf.draw.HemfDrawProperties;
 import org.apache.poi.hemf.draw.HemfGraphics;
 import org.apache.poi.hemf.record.emf.HemfFill;
@@ -51,6 +52,7 @@ import 
org.apache.poi.hwmf.record.HwmfMisc.WmfSetBkMode.HwmfBkMode;
 import org.apache.poi.hwmf.record.HwmfPenStyle;
 import org.apache.poi.hwmf.record.HwmfTernaryRasterOp;
 import org.apache.poi.hwmf.record.HwmfText;
+import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.sl.draw.ImageRenderer;
 import org.apache.poi.util.BitField;
 import org.apache.poi.util.BitFieldFactory;
@@ -63,6 +65,8 @@ import org.apache.poi.util.StringUtil;
 
 @SuppressWarnings("WeakerAccess")
 public final class HemfPlusDraw {
+    private static final Logger LOG = 
PoiLogManager.getLogger(HemfPlusDraw.class);
+
     private static final int MAX_OBJECT_SIZE = 1_000_000;
 
     private HemfPlusDraw() {}
@@ -204,9 +208,12 @@ public final class HemfPlusDraw {
             // A 32-bit unsigned integer that specifies an index in the EMF+ 
Object Table for an EmfPlusPen object
             // to use for drawing the EmfPlusPath. The value MUST be zero to 
63, inclusive.
             penId = leis.readInt();
-            assert (0 <= penId && penId <= 63);
-
-            assert (dataSize == LittleEndianConsts.INT_SIZE);
+            if (penId < 0 || penId > 63) {
+                LOG.atWarn().log("Unexpected pen id in emf+ file: {}", penId);
+            }
+            if (dataSize != LittleEndianConsts.INT_SIZE) {
+                LOG.atWarn().log("Unexpected data size of DrawPath record in 
emf+ file: {}", dataSize);
+            }
 
             return LittleEndianConsts.INT_SIZE;
         }
@@ -370,7 +377,9 @@ public final class HemfPlusDraw {
             // A 32-bit signed integer that defines the units of the SrcRect 
field.
             // It MUST be the UnitPixel value of the UnitType enumeration
             srcUnit = EmfPlusUnitType.valueOf(leis.readInt());
-            assert(srcUnit == EmfPlusUnitType.Pixel);
+            if (srcUnit != EmfPlusUnitType.Pixel) {
+                LOG.atWarn().log("Unexpected source unit of DrawImagePoints 
record in emf+ file: {}", srcUnit);
+            }
 
             int size = 2 * LittleEndianConsts.INT_SIZE;
 
@@ -380,7 +389,9 @@ public final class HemfPlusDraw {
             // A 32-bit unsigned integer that specifies the number of points 
in the PointData array.
             // Exactly 3 points MUST be specified.
             int count = leis.readInt();
-            assert(count == 3);
+            if (count != 3) {
+                LOG.atWarn().log("Unexpected point count of DrawImagePoints 
record in emf+ file: {}", count);
+            }
             size += LittleEndianConsts.INT_SIZE;
 
             BiFunction<LittleEndianInputStream, Point2D, Integer> readPoint;
@@ -518,7 +529,9 @@ public final class HemfPlusDraw {
             // A 32-bit signed integer that defines the units of the SrcRect 
field.
             // It MUST be the UnitPixel value of the UnitType enumeration
             srcUnit = EmfPlusUnitType.valueOf(leis.readInt());
-            assert(srcUnit == EmfPlusUnitType.Pixel);
+            if (srcUnit != EmfPlusUnitType.Pixel) {
+                LOG.atWarn().log("Unexpected source unit of DrawImage record 
in emf+ file: {}", srcUnit);
+            }
 
             int size = 2 * LittleEndianConsts.INT_SIZE;
 
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 121fe87f68..ab3d23fff8 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
@@ -396,13 +396,12 @@ public class HemfPlusImage {
                     bitmapType = null;
                     pixelFormat = null;
 
-                    fileSize = dataSize;
+                    // the version and type of an unknown image were read 
already
+                    fileSize = dataSize - size;
                     break;
 
             }
 
-            assert (fileSize <= dataSize - size);
-
             imageData = IOUtils.toByteArray(leis, fileSize, MAX_OBJECT_SIZE);
 
             // TODO: remove padding bytes between placeable WMF header and 
body?
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRecordIterator.java
 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRecordIterator.java
index ad076ed130..1f3279cf59 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRecordIterator.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hemf/record/emfplus/HemfPlusRecordIterator.java
@@ -85,7 +85,9 @@ public class HemfPlusRecordIterator implements 
Iterator<HemfPlusRecord> {
 
         try {
             long readBytes = record.init(leis, dataSize, recordId, flags);
-            assert (readBytes <= recordSize-12);
+            if (readBytes > recordSize-12) {
+                throw new RecordFormatException("Record limit exceeded - 
readBytes: "+readBytes+" / remBytes: "+(recordSize-12));
+            }
             leis.skipFully((int)(recordSize-12-readBytes));
         } catch (IOException e) {
             throw new RecordFormatException(e);
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 dce21107dd..10dfa94645 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
@@ -27,6 +27,7 @@ import java.util.NoSuchElementException;
 import javax.imageio.ImageIO;
 
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
+import org.apache.logging.log4j.Logger;
 import org.apache.poi.hemf.record.emf.HemfComment;
 import org.apache.poi.hemf.record.emf.HemfComment.EmfComment;
 import org.apache.poi.hemf.record.emf.HemfComment.EmfCommentDataFormat;
@@ -44,10 +45,13 @@ import org.apache.poi.hwmf.record.HwmfBitmapDib;
 import org.apache.poi.hwmf.record.HwmfFill;
 import org.apache.poi.hwmf.usermodel.HwmfEmbedded;
 import org.apache.poi.hwmf.usermodel.HwmfEmbeddedType;
+import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.poifs.filesystem.FileMagic;
 import org.apache.poi.util.IOUtils;
 
 public class HemfEmbeddedIterator implements Iterator<HwmfEmbedded> {
+    private static final Logger LOG = 
PoiLogManager.getLogger(HemfEmbeddedIterator.class);
+
     //arbitrarily selected; may need to increase
     private static final int DEFAULT_MAX_RECORD_LENGTH = 100_000_000;
     private static int MAX_RECORD_LENGTH = DEFAULT_MAX_RECORD_LENGTH;
@@ -306,7 +310,9 @@ public class HemfEmbeddedIterator implements 
Iterator<HwmfEmbedded> {
             boolean hasNext = false;
             do {
                 EmfPlusImage img = epo.getObjectData();
-                assert(!hasNext || img.getImageDataType() == 
HemfPlusImage.EmfPlusImageDataType.CONTINUED);
+                if (hasNext && img.getImageDataType() != 
HemfPlusImage.EmfPlusImageDataType.CONTINUED) {
+                    LOG.atWarn().log("Unexpected image data type of continued 
emf+ image: {}", img.getImageDataType());
+                }
                 bos.write(img.getImageData());
                 current = null;
                 hasNext = moveNext() &&
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java 
b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java
index 30e8f5a8fc..e675936a06 100644
--- a/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java
+++ b/poi-scratchpad/src/main/java/org/apache/poi/hwmf/record/HwmfBitmapDib.java
@@ -254,8 +254,6 @@ public class HwmfBitmapDib implements GenericRecord {
         // BI_RGB, BI_BITFIELDS, or BI_CMYK.
         // Otherwise, the size of aData MUST be the BitmapInfoHeader Object 
value ImageSize.
 
-        assert( headerSize != 0x0C || bodySize == headerImageSize);
-
         if (headerSize == 0x0C ||
             headerCompression == Compression.BI_RGB ||
             headerCompression == Compression.BI_BITFIELDS ||
@@ -302,6 +300,8 @@ public class HwmfBitmapDib implements GenericRecord {
             // A 16-bit unsigned integer that defines the format of each 
pixel, and the
             // maximum number of colors in the DIB.
             headerBitCount = BitCount.valueOf(leis.readUShort());
+            // a BitmapCoreHeader has no compression field, its bitmaps are 
uncompressed
+            headerCompression = Compression.BI_RGB;
             size += 4*LittleEndianConsts.SHORT_SIZE;
         } else {
             // fix header size, sometimes this is invalid
@@ -374,6 +374,7 @@ public class HwmfBitmapDib implements GenericRecord {
             // 256 colors
             return readRGBQuad(leis, headerColorUsed == 0 ? 256 : 
Math.toIntExact(Math.min(headerColorUsed,256)));
         case BI_BITCOUNT_4:
+            checkCompression();
             switch (headerCompression) {
             case BI_RGB:
                 colorMaskB = 0x1F;
@@ -390,6 +391,7 @@ public class HwmfBitmapDib implements GenericRecord {
             }
         case BI_BITCOUNT_5:
         case BI_BITCOUNT_6:
+            checkCompression();
             switch (headerCompression) {
             case BI_RGB:
                 colorMaskR=0xFF;
@@ -407,6 +409,13 @@ public class HwmfBitmapDib implements GenericRecord {
         }
     }
 
+    private void checkCompression() throws IOException {
+        // an unknown compression value is read as null
+        if (headerCompression == null) {
+            throw new IOException("Invalid compression option (unknown) for 
bitcount ("+headerBitCount+").");
+        }
+    }
+
     protected int readRGBQuad(LittleEndianInputStream leis, int count) throws 
IOException {
         int size = 0;
         colorTable = new Color[count];
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 c93bb13fde..e70454bd9e 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
@@ -29,9 +29,11 @@ import java.io.IOException;
 import java.util.Map;
 import java.util.function.Supplier;
 
+import org.apache.logging.log4j.Logger;
 import org.apache.poi.hwmf.draw.HwmfDrawProperties;
 import org.apache.poi.hwmf.draw.HwmfGraphics;
 import org.apache.poi.hwmf.record.HwmfMisc.WmfSetBkMode.HwmfBkMode;
+import org.apache.poi.logging.PoiLogManager;
 import org.apache.poi.sl.draw.ImageRenderer;
 import org.apache.poi.util.GenericRecordJsonWriter;
 import org.apache.poi.util.GenericRecordUtil;
@@ -40,6 +42,8 @@ import org.apache.poi.util.LittleEndianInputStream;
 
 @SuppressWarnings("WeakerAccess")
 public class HwmfFill {
+    private static final Logger LOG = PoiLogManager.getLogger(HwmfFill.class);
+
     /** A record which contains an image (to be extracted) */
     public interface HwmfImageRecord {
 
@@ -986,7 +990,9 @@ public class HwmfFill {
 
         // null for an unknown raster operation, which is drawn like SRCCOPY
         HwmfTernaryRasterOp rasterOperation = 
HwmfTernaryRasterOp.valueOf(rasterOpIndex);
-        assert(rasterOperation == null || rasterOpCode == 
rasterOperation.getOpCode());
+        if (rasterOperation != null && rasterOpCode != 
rasterOperation.getOpCode()) {
+            LOG.atWarn().log("Unexpected op code {} for raster operation {} in 
wmf file", rasterOpCode, rasterOperation);
+        }
         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
index dc3d6d2b67..4a4b58bee6 100644
--- 
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
@@ -17,6 +17,7 @@
 
 package org.apache.poi.hemf.record.emfplus;
 
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -26,6 +27,9 @@ import java.io.IOException;
 import java.util.Collections;
 
 import org.apache.poi.hemf.record.emfplus.HemfPlusBrush.EmfPlusBrush;
+import org.apache.poi.hemf.record.emfplus.HemfPlusDraw.EmfPlusDrawPath;
+import org.apache.poi.hemf.record.emfplus.HemfPlusImage.EmfPlusImage;
+import org.apache.poi.hemf.record.emfplus.HemfPlusImage.EmfPlusImageDataType;
 import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObject;
 import org.apache.poi.hemf.record.emfplus.HemfPlusObject.EmfPlusObjectType;
 import org.apache.poi.util.LittleEndian;
@@ -63,4 +67,48 @@ class TestHemfPlusObject {
             () -> brush.getBrushData(Collections.emptyList()));
         assertEquals("Unknown brush type", ex.getMessage());
     }
+
+    @Test
+    void readsUnknownImageType() throws IOException {
+        byte[] data = new byte[12];
+        // EmfPlusGraphicsVersion: metafile signature 0xDBC01, graphics 
version 1
+        LittleEndian.putInt(data, 0, 0xDBC01001);
+        // image data type 0x7F isn't defined
+        LittleEndian.putInt(data, 4, 0x7F);
+        LittleEndian.putInt(data, 8, 0x04030201);
+
+        EmfPlusImage img = new EmfPlusImage();
+        try (LittleEndianInputStream leis = new LittleEndianInputStream(new 
ByteArrayInputStream(data))) {
+            // the image data is what follows the version and type, not the 
whole record
+            assertEquals(data.length, img.init(leis, data.length, 
EmfPlusObjectType.IMAGE, 0));
+        }
+        assertEquals(EmfPlusImageDataType.UNKNOWN, img.getImageDataType());
+        assertArrayEquals(new byte[]{1, 2, 3, 4}, img.getImageData());
+    }
+
+    @Test
+    void rejectsRecordReadingPastItsSize() {
+        byte[] data = new byte[16];
+        LittleEndian.putUShort(data, 0, (int) HemfPlusRecordType.drawPath.id);
+        // the record size only covers the header, but DrawPath reads a 4 byte 
pen id
+        LittleEndian.putInt(data, 4, 12);
+        LittleEndian.putInt(data, 8, 4);
+
+        RecordFormatException ex = assertThrows(RecordFormatException.class,
+            () -> new HemfPlusRecordIterator(new LittleEndianInputStream(new 
ByteArrayInputStream(data))));
+        assertEquals("Record limit exceeded - readBytes: 4 / remBytes: 0", 
ex.getMessage());
+    }
+
+    @Test
+    void readsPenIdOutsideTheSpec() throws IOException {
+        byte[] data = new byte[4];
+        // the spec allows 0 to 63, which parsing only warns about
+        LittleEndian.putInt(data, 0, 100);
+
+        EmfPlusDrawPath drawPath = new EmfPlusDrawPath();
+        try (LittleEndianInputStream leis = new LittleEndianInputStream(new 
ByteArrayInputStream(data))) {
+            drawPath.init(leis, data.length, HemfPlusRecordType.drawPath.id, 
0);
+        }
+        assertEquals(100, drawPath.getPenId());
+    }
 }
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfBitmapDib.java 
b/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfBitmapDib.java
new file mode 100644
index 0000000000..6a59bddea8
--- /dev/null
+++ b/poi-scratchpad/src/test/java/org/apache/poi/hwmf/TestHwmfBitmapDib.java
@@ -0,0 +1,67 @@
+/* ====================================================================
+   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 static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import org.apache.poi.hwmf.record.HwmfBitmapDib;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.LittleEndianInputStream;
+import org.junit.jupiter.api.Test;
+
+class TestHwmfBitmapDib {
+
+    @Test
+    void readsCoreHeader() throws IOException {
+        byte[] data = new byte[16];
+        // BitmapCoreHeader: size, 1x1 pixel, 1 plane, 24 bits per pixel
+        LittleEndian.putInt(data, 0, 0x0C);
+        LittleEndian.putUShort(data, 4, 1);
+        LittleEndian.putUShort(data, 6, 1);
+        LittleEndian.putUShort(data, 8, 1);
+        LittleEndian.putUShort(data, 10, 24);
+        // one pixel, padded to 4 bytes
+        data[12] = (byte) 0xFF;
+
+        HwmfBitmapDib dib = new HwmfBitmapDib();
+        try (LittleEndianInputStream leis = new LittleEndianInputStream(new 
ByteArrayInputStream(data))) {
+            assertEquals(data.length, dib.init(leis, data.length));
+        }
+    }
+
+    @Test
+    void rejectsUnknownCompression() {
+        byte[] data = new byte[44];
+        // BitmapInfoHeader: size, 1x1 pixel, 1 plane, 24 bits per pixel, 
compression 0x7F isn't defined
+        LittleEndian.putInt(data, 0, 40);
+        LittleEndian.putInt(data, 4, 1);
+        LittleEndian.putInt(data, 8, 1);
+        LittleEndian.putUShort(data, 12, 1);
+        LittleEndian.putUShort(data, 14, 24);
+        LittleEndian.putInt(data, 16, 0x7F);
+
+        HwmfBitmapDib dib = new HwmfBitmapDib();
+        LittleEndianInputStream leis = new LittleEndianInputStream(new 
ByteArrayInputStream(data));
+        IOException ex = assertThrows(IOException.class, () -> dib.init(leis, 
data.length));
+        assertEquals("Invalid compression option (unknown) for bitcount 
(BI_BITCOUNT_5).", ex.getMessage());
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to