From da7516bddb196774de4322b8a9e320b17018630b Mon Sep 17 00:00:00 2001 From: Evangelos Foutras Date: Mon, 2 Apr 2018 16:09:34 +0300 Subject: [PATCH v2] Fix PDFDoc::checkHeader() for PDFs smaller than 1 KiB The fix for bug 104502 made it so PDFDoc::checkHeader() would print a warning and return immediatelly if it encounters an EOF while reading the first 1024 bytes. Some PDF files can be smaller than 1024 bytes, for example those used by pdf2djvu's test suite. The latter would fail due to the unexpected warnings. Change the behavior of PDFDoc::checkHeader() when encountering an EOF so it processes the data it has read so far instead of aborting early. https://bugs.freedesktop.org/show_bug.cgi?id=105674 --- poppler/PDFDoc.cc | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index 6789c39f..656493ed 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -410,23 +410,30 @@ void PDFDoc::checkHeader() { char *tokptr; int i; int c; + int bytesRead; pdfMajorVersion = 0; pdfMinorVersion = 0; + + // read up to headerSearchSize bytes from the beginning of the document for (i = 0; i < headerSearchSize; ++i) { - if ((c = str->getChar()) == EOF) { - error(errSyntaxWarning, -1, "EOF while reading header (continuing anyway)"); - return; - } + c = str->getChar(); + if (c == EOF) + break; hdrBuf[i] = c; } - hdrBuf[headerSearchSize] = '\0'; - for (i = 0; i < headerSearchSize - 5; ++i) { + bytesRead = i; + hdrBuf[bytesRead] = '\0'; + + // find the start of the PDF header if it exists and parse the version + bool headerFound = false; + for (i = 0; i < bytesRead - 5; ++i) { if (!strncmp(&hdrBuf[i], "%PDF-", 5)) { + headerFound = true; break; } } - if (i >= headerSearchSize - 5) { + if (!headerFound) { error(errSyntaxWarning, -1, "May not be a PDF file (continuing anyway)"); return; } -- 2.16.3