From 868691020eceadf634406362ad0605c59d7a576b Mon Sep 17 00:00:00 2001 From: Alfredo Garcia Date: Mon, 17 Feb 2020 17:46:32 -0300 Subject: [PATCH 1/3] Add null check to feof. Co-authored-by: Jack Grigg Co-authored-by: Ying Tong Lai Co-authored-by: Daira Hopwood --- src/streams.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/streams.h b/src/streams.h index 5dbcfaf21..24051e11d 100644 --- a/src/streams.h +++ b/src/streams.h @@ -566,7 +566,7 @@ public: // check whether we're at the end of the source file bool eof() const { - return nReadPos == nSrcPos && feof(src); + return src == NULL || (nReadPos == nSrcPos && feof(src)); } // read a number of bytes From 6c20de21b9606f6f151cda48e9179883c212da85 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Wed, 26 Aug 2020 13:10:28 +0100 Subject: [PATCH 2/3] CBufferedFile: assert that Fill() is only called when nReadPos == nSrcPos, and simplify based on that assumption. Signed-off-by: Daira Hopwood --- src/streams.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/streams.h b/src/streams.h index 24051e11d..3673faa1b 100644 --- a/src/streams.h +++ b/src/streams.h @@ -524,9 +524,10 @@ private: protected: // read data from the source to fill the buffer bool Fill() { + assert(nReadPos == nSrcPos); unsigned int pos = nSrcPos % vchBuf.size(); unsigned int readNow = vchBuf.size() - pos; - unsigned int nAvail = vchBuf.size() - (nSrcPos - nReadPos) - nRewind; + unsigned int nAvail = vchBuf.size() - nRewind; if (nAvail < readNow) readNow = nAvail; if (readNow == 0) From fb8dd9d4589e63003ea77ff71a227ab6885f4260 Mon Sep 17 00:00:00 2001 From: Daira Hopwood Date: Wed, 26 Aug 2020 13:11:17 +0100 Subject: [PATCH 3/3] CBufferedFile: use eof() method rather than feof(src) in error message. Co-authored-by: Jack Grigg Signed-off-by: Daira Hopwood --- src/streams.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/streams.h b/src/streams.h index 3673faa1b..8680cde5b 100644 --- a/src/streams.h +++ b/src/streams.h @@ -534,7 +534,7 @@ protected: return false; size_t nBytes = fread((void*)&vchBuf[pos], 1, readNow, src); if (nBytes == 0) { - throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed"); + throw std::ios_base::failure(eof() ? "CBufferedFile::Fill: end of file" : "CBufferedFile::Fill: fread failed"); } nSrcPos += nBytes; return true;