From b7944d08a38f3713977606d3bc4d3ac45e72798c Mon Sep 17 00:00:00 2001 From: pommicket Date: Thu, 11 Sep 2025 21:39:38 -0400 Subject: Check for short read --- pom.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pom.c b/pom.c index a46e430..651d308 100644 --- a/pom.c +++ b/pom.c @@ -95,7 +95,7 @@ struct parser { char *array; size_t count, capacity; } error_messages; - bool eof, out_of_memory, leftover_cr; + bool short_read, eof, out_of_memory, leftover_cr; // see enum utf8_state -- starting state for future calls to read_func uint8_t utf8_state; uint16_t buf_pos; @@ -274,18 +274,23 @@ parser_error(struct parser *parser, const char *fmt, ...) { static bool parser_read_to_buf(struct parser *parser, bool skip_bom) { if (parser->eof) return false; - char *buf = parser->buf; - size_t read_count = parser->read_func(parser->userdata, buf, sizeof parser->buf - 1); - parser->buf_pos = 0; uint8_t utf8_state = parser->utf8_state; - if (read_count == 0) { + if (parser->short_read) { // last read was short, so we're at EOF // EOF reached. + eof: if (utf8_state) { parser_error(parser, "Invalid UTF-8 (want continuation byte, got EOF)."); } parser->eof = true; return false; } + char *buf = parser->buf; + size_t read_count = parser->read_func(parser->userdata, buf, sizeof parser->buf - 1); + parser->buf_pos = 0; + if (read_count == 0) + goto eof; + if (read_count < sizeof parser->buf - 1) + parser->short_read = true; parser->utf8_state = utf8_state; if (parser->leftover_cr && buf[0] != '\n') parser_error(parser, "Carriage return with no newline after it."); -- cgit v1.2.3