poppler/Form.cc | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-)
New commits: commit 3928dde50bfd44d340ef0cb302610cbcf9bece58 Author: Albert Astals Cid <aa...@kde.org> Date: Sun Jan 3 00:09:22 2021 +0100 Account for fread potentially failing diff --git a/poppler/Form.cc b/poppler/Form.cc index 0941f4b7..06855785 100644 --- a/poppler/Form.cc +++ b/poppler/Form.cc @@ -510,7 +510,9 @@ static bool hashFileRange(FILE *f, SignatureHandler *handler, Goffset start, Gof int len = BUF_SIZE; if (end - start < len) len = end - start; - fread(buf, len, 1, f); + if (fread(buf, 1, len, f) != static_cast<size_t>(len)) { + return false; + } handler->updateHash(buf, len); start += len; } @@ -657,19 +659,21 @@ bool FormWidgetSignature::updateOffsets(FILE *f, Goffset objStart, Goffset objEn return false; } - int bufSize = static_cast<int>(objEnd - objStart); + const size_t bufSize = static_cast<int>(objEnd - objStart); if (Gfseek(f, objStart, SEEK_SET) != 0) { return false; } std::vector<char> buf(bufSize + 1); - fread(buf.data(), bufSize, 1, f); + if (fread(buf.data(), 1, bufSize, f) != bufSize) { + return false; + } buf[bufSize] = 0; // prevent string functions from searching past the end // search for the Contents field which contains the signature // which always must start with hex digits 308 *sigStart = -1; *sigEnd = -1; - for (int i = 0; i < bufSize - 14; i++) { + for (size_t i = 0; i < bufSize - 14; i++) { if (buf[i] == '/' && strncmp(&buf[i], "/Contents <308", 14) == 0) { *sigStart = objStart + i + 10; char *p = strchr(&buf[i], '>'); @@ -683,7 +687,7 @@ bool FormWidgetSignature::updateOffsets(FILE *f, Goffset objStart, Goffset objEn return false; // Search for ByteRange array and update offsets - for (int i = 0; i < bufSize - 10; i++) { + for (size_t i = 0; i < bufSize - 10; i++) { if (buf[i] == '/' && strncmp(&buf[i], "/ByteRange", 10) == 0) { // update range char *p = setNextOffset(&buf[i], *sigStart); commit ec9420b76c6a1dfc7101b03f475fa0c91fa49a47 Author: Albert Astals Cid <aa...@kde.org> Date: Sun Jan 3 00:04:17 2021 +0100 Account for Gfseek potentially failing diff --git a/poppler/Form.cc b/poppler/Form.cc index 5ee2d514..0941f4b7 100644 --- a/poppler/Form.cc +++ b/poppler/Form.cc @@ -496,14 +496,17 @@ SignatureInfo *FormWidgetSignature::validateSignature(bool doVerifyCert, bool fo #ifdef ENABLE_NSS3 // update hash with the specified range of data from the file -static void hashFileRange(FILE *f, SignatureHandler *handler, Goffset start, Goffset end) +static bool hashFileRange(FILE *f, SignatureHandler *handler, Goffset start, Goffset end) { const int BUF_SIZE = 65536; unsigned char *buf = new unsigned char[BUF_SIZE]; while (start < end) { - Gfseek(f, start, SEEK_SET); + if (Gfseek(f, start, SEEK_SET) != 0) { + delete[] buf; + return false; + } int len = BUF_SIZE; if (end - start < len) len = end - start; @@ -512,6 +515,7 @@ static void hashFileRange(FILE *f, SignatureHandler *handler, Goffset start, Gof start += len; } delete[] buf; + return true; } #endif @@ -570,8 +574,14 @@ bool FormWidgetSignature::signDocument(const char *saveFilename, const char *cer // compute hash of byte ranges sigHandler.restartHash(); - hashFileRange(file, &sigHandler, 0LL, sigStart); - hashFileRange(file, &sigHandler, sigEnd, fileSize); + if (!hashFileRange(file, &sigHandler, 0LL, sigStart)) { + fclose(file); + return false; + } + if (!hashFileRange(file, &sigHandler, sigEnd, fileSize)) { + fclose(file); + return false; + } // and sign it const std::unique_ptr<GooString> signature = sigHandler.signDetached(password); @@ -634,7 +644,9 @@ static char *setNextOffset(char *start, Goffset offset) // Returns start/end of signature string and file size. bool FormWidgetSignature::updateOffsets(FILE *f, Goffset objStart, Goffset objEnd, Goffset *sigStart, Goffset *sigEnd, Goffset *fileSize) { - Gfseek(f, 0, SEEK_END); + if (Gfseek(f, 0, SEEK_END) != 0) { + return false; + } *fileSize = Gftell(f); if (objEnd > *fileSize) @@ -646,7 +658,9 @@ bool FormWidgetSignature::updateOffsets(FILE *f, Goffset objStart, Goffset objEn } int bufSize = static_cast<int>(objEnd - objStart); - Gfseek(f, objStart, SEEK_SET); + if (Gfseek(f, objStart, SEEK_SET) != 0) { + return false; + } std::vector<char> buf(bufSize + 1); fread(buf.data(), bufSize, 1, f); buf[bufSize] = 0; // prevent string functions from searching past the end @@ -686,7 +700,9 @@ bool FormWidgetSignature::updateOffsets(FILE *f, Goffset objStart, Goffset objEn } // write buffer back to disk - Gfseek(f, objStart, SEEK_SET); + if (Gfseek(f, objStart, SEEK_SET) != 0) { + return false; + } fwrite(buf.data(), bufSize, 1, f); return true; } @@ -697,7 +713,9 @@ bool FormWidgetSignature::updateSignature(FILE *f, Goffset sigStart, Goffset sig if (signature->getLength() * 2 + 2 != sigEnd - sigStart) return false; - Gfseek(f, sigStart, SEEK_SET); + if (Gfseek(f, sigStart, SEEK_SET) != 0) { + return false; + } const char *c = signature->c_str(); fprintf(f, "<"); for (int i = 0; i < signature->getLength(); i++) { _______________________________________________ poppler mailing list poppler@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/poppler