Skip to content

Commit

Permalink
fix: fail gracefully when running on a non-EPUB file (#1134)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rdeltour authored May 1, 2020
1 parent f115730 commit 2083f05
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/main/java/com/adobe/epubcheck/api/EpubCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ void checkExtension(OCFPackage ocf, String extension)
else
{
List<String> opfPaths = ocf.getOcfData().getEntries(OPFData.OPF_MIME_TYPE);
if(ocf.getOpfData().get(opfPaths.get(0)).getVersion() == EPUBVersion.VERSION_3) {
report.message(MessageId.PKG_024, EPUBLocation.create(epubFile.getName(), extension));
} else {
report.message(MessageId.PKG_017, EPUBLocation.create(epubFile.getName(), extension));
if (!opfPaths.isEmpty()) {
if(ocf.getOpfData().get(opfPaths.get(0)).getVersion() == EPUBVersion.VERSION_3) {
report.message(MessageId.PKG_024, EPUBLocation.create(epubFile.getName(), extension));
} else {
report.message(MessageId.PKG_017, EPUBLocation.create(epubFile.getName(), extension));
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/adobe/epubcheck/xml/XMLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ public void process()
try
{
InputStream in = closer.register(context.resourceProvider.getInputStream(path));
if (in == null) {
return; // Abort processing. Missing required files are reported elsewhere.
}
// System.err.println("DEBUG XMLParser#process on" + resource);
if (!in.markSupported())
{
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/adobe/epubcheck/api/Epub30CheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,12 @@ public void testEdupubRenditions_Invalid_NoRenditionDCType()
Collections.addAll(expectedErrors, MessageId.RSC_005);
testValidateDocument("invalid/edupub-multiple-renditions-nodctype-rendition.epub");
}

@Test
public void testNotAnEPUB()
{
Collections.addAll(expectedFatals, MessageId.RSC_002);
Collections.addAll(expectedErrors, MessageId.PKG_006, MessageId.RSC_001);
testValidateDocument("invalid/not-an-epub.docx");
}
}
Binary file not shown.

0 comments on commit 2083f05

Please sign in to comment.