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).
This commit is contained in:
Michael Hansen
2025-08-28 15:14:55 -07:00
parent a267bfb47f
commit ff0c1450b4

View File

@@ -53,8 +53,10 @@ bool PycFile::atEof() const
int PycFile::getByte() int PycFile::getByte()
{ {
int ch = fgetc(m_stream); int ch = fgetc(m_stream);
if (ch == EOF) if (ch == EOF) {
ungetc(ch, m_stream); fputs("PycFile::getByte(): Unexpected end of stream\n", stderr);
std::exit(1);
}
return ch; return ch;
} }
@@ -67,8 +69,10 @@ int PycFile::getBuffer(int bytes, void* buffer)
/* PycBuffer */ /* PycBuffer */
int PycBuffer::getByte() int PycBuffer::getByte()
{ {
if (atEof()) if (atEof()) {
return EOF; fputs("PycBuffer::getByte(): Unexpected end of stream\n", stderr);
std::exit(1);
}
int ch = (int)(*(m_buffer + m_pos)); int ch = (int)(*(m_buffer + m_pos));
++m_pos; ++m_pos;
return ch & 0xFF; // Make sure it's just a byte! return ch & 0xFF; // Make sure it's just a byte!