Author: tilman
Date: Thu Aug 27 11:39:56 2026
New Revision: 1937519
Log:
PDFBOX-5660: improve test coverage
Modified:
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/pfb/PfbParser.java
pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java
Modified:
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/pfb/PfbParser.java
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/pfb/PfbParser.java
Thu Aug 27 11:39:51 2026 (r1937518)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/pfb/PfbParser.java
Thu Aug 27 11:39:56 2026 (r1937519)
@@ -44,27 +44,27 @@ public class PfbParser
* (start-marker (1 byte), ascii-/binary-marker (1 byte), size (4 byte))
* 3*6 == 18
*/
- private static final int PFB_HEADER_LENGTH = 18;
+ static final int PFB_HEADER_LENGTH = 18;
/**
* the start marker.
*/
- private static final int START_MARKER = 0x80;
+ static final int START_MARKER = 0x80;
/**
* the ascii marker.
*/
- private static final int ASCII_MARKER = 0x01;
+ static final int ASCII_MARKER = 0x01;
/**
* the binary marker.
*/
- private static final int BINARY_MARKER = 0x02;
+ static final int BINARY_MARKER = 0x02;
/**
* the EOF marker.
*/
- private static final int EOF_MARKER = 0x03;
+ static final int EOF_MARKER = 0x03;
/**
* the parsed pfb-data.
Modified:
pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java
==============================================================================
---
pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java
Thu Aug 27 11:39:51 2026 (r1937518)
+++
pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/pfb/PfbParserTest.java
Thu Aug 27 11:39:56 2026 (r1937519)
@@ -99,7 +99,26 @@ class PfbParserTest
@Test
void testEmpty()
{
- Assertions.assertThrows(IOException.class, () ->
Type1Font.createWithPFB(new byte[0]));
+ IOException ex1 = Assertions.assertThrows(IOException.class,
+ () -> Type1Font.createWithPFB(new byte[0]));
+ Assertions.assertEquals("PFB header missing", ex1.getMessage());
+ }
+
+ /**
+ * Test some bad fonts.
+ */
+ @Test
+ void testMiscBadFonts()
+ {
+ byte[] ba = new byte[PfbParser.PFB_HEADER_LENGTH + 1];
+ IOException ex1 = Assertions.assertThrows(IOException.class,
+ () -> Type1Font.createWithPFB(ba));
+ Assertions.assertEquals("Start marker missing", ex1.getMessage());
+ ba[0] = (byte) PfbParser.START_MARKER;
+ ba[1] = 33;
+ IOException ex2 = Assertions.assertThrows(IOException.class,
+ () -> Type1Font.createWithPFB(ba));
+ Assertions.assertEquals("Incorrect record type: 33", ex2.getMessage());
}
/**
@@ -120,6 +139,27 @@ class PfbParserTest
0x27, 0x05, (byte) 0xF8, (byte) 0xFF,
(byte) 0xD2, 0x40
};
- Assertions.assertThrows(IOException.class, () -> new
PfbParser(crashInput));
+ IOException ex = Assertions.assertThrows(IOException.class, () -> new
PfbParser(crashInput));
+ Assertions.assertEquals("record size -16777215 is negative",
ex.getMessage());
+ }
+
+ /**
+ * Test that a PFB with a high size field throws an exception
+ */
+ @Test
+ void testHighRecordSize()
+ {
+ // 18-byte crafted PFB: start marker 0x80, ASCII type 0x01,
+ // size field 0x7f 0x00 0x00 0x00 = 0x7f
+ byte[] crashInput = {
+ (byte) 0x80, 0x01, // header
+ 0x7f, 0x00, 0x00, 0x00, // size too high
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, // garbage data
+ (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
+ 0x27, 0x05, (byte) 0xF8, (byte) 0xFF,
+ (byte) 0xD2, 0x40
+ };
+ IOException ex = Assertions.assertThrows(IOException.class, () -> new
PfbParser(crashInput));
+ Assertions.assertEquals("record size 127 would be larger than the
input", ex.getMessage());
}
}