From ff0c1450b49d5228bc7078178799fe6c95bd22e9 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Thu, 28 Aug 2025 15:14:55 -0700 Subject: [PATCH] Abort immediately when attempting to read past end of stream. No consumers of readByte() were actually checking for EOF, so they would all keep re-reading the same byte over and over again, potentially until the process runs out of memory (ref #572). --- data.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/data.cpp b/data.cpp index 1be5aa6..9509a29 100644 --- a/data.cpp +++ b/data.cpp @@ -53,8 +53,10 @@ bool PycFile::atEof() const int PycFile::getByte() { int ch = fgetc(m_stream); - if (ch == EOF) - ungetc(ch, m_stream); + if (ch == EOF) { + fputs("PycFile::getByte(): Unexpected end of stream\n", stderr); + std::exit(1); + } return ch; } @@ -67,8 +69,10 @@ int PycFile::getBuffer(int bytes, void* buffer) /* PycBuffer */ int PycBuffer::getByte() { - if (atEof()) - return EOF; + if (atEof()) { + fputs("PycBuffer::getByte(): Unexpected end of stream\n", stderr); + std::exit(1); + } int ch = (int)(*(m_buffer + m_pos)); ++m_pos; return ch & 0xFF; // Make sure it's just a byte!