Skip to content

Commit 2083f05

Browse files
authored
fix: fail gracefully when running on a non-EPUB file (#1134)
Running EPUBCheck on a non-EPUB package file (for instance an Office Open XML document) caused a a `NullPointerException`. It now fails gracefully with a fatal error (RSC-002) potentially followed by side-effect errors (typically RSC-001, PKG-006). Fixes #1050
1 parent f115730 commit 2083f05

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

src/main/java/com/adobe/epubcheck/api/EpubCheck.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ void checkExtension(OCFPackage ocf, String extension)
268268
else
269269
{
270270
List<String> opfPaths = ocf.getOcfData().getEntries(OPFData.OPF_MIME_TYPE);
271-
if(ocf.getOpfData().get(opfPaths.get(0)).getVersion() == EPUBVersion.VERSION_3) {
272-
report.message(MessageId.PKG_024, EPUBLocation.create(epubFile.getName(), extension));
273-
} else {
274-
report.message(MessageId.PKG_017, EPUBLocation.create(epubFile.getName(), extension));
271+
if (!opfPaths.isEmpty()) {
272+
if(ocf.getOpfData().get(opfPaths.get(0)).getVersion() == EPUBVersion.VERSION_3) {
273+
report.message(MessageId.PKG_024, EPUBLocation.create(epubFile.getName(), extension));
274+
} else {
275+
report.message(MessageId.PKG_017, EPUBLocation.create(epubFile.getName(), extension));
276+
}
275277
}
276278
}
277279
}

src/main/java/com/adobe/epubcheck/xml/XMLParser.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ public void process()
196196
try
197197
{
198198
InputStream in = closer.register(context.resourceProvider.getInputStream(path));
199+
if (in == null) {
200+
return; // Abort processing. Missing required files are reported elsewhere.
201+
}
199202
// System.err.println("DEBUG XMLParser#process on" + resource);
200203
if (!in.markSupported())
201204
{

src/test/java/com/adobe/epubcheck/api/Epub30CheckTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,12 @@ public void testEdupubRenditions_Invalid_NoRenditionDCType()
367367
Collections.addAll(expectedErrors, MessageId.RSC_005);
368368
testValidateDocument("invalid/edupub-multiple-renditions-nodctype-rendition.epub");
369369
}
370+
371+
@Test
372+
public void testNotAnEPUB()
373+
{
374+
Collections.addAll(expectedFatals, MessageId.RSC_002);
375+
Collections.addAll(expectedErrors, MessageId.PKG_006, MessageId.RSC_001);
376+
testValidateDocument("invalid/not-an-epub.docx");
377+
}
370378
}
Binary file not shown.

0 commit comments

Comments
 (0)