From 4920b1dd988532111865b5e6263e5bcac21d8c18 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 28 Nov 2023 04:33:22 +0100 Subject: [PATCH] ttyplot.c: Truncate the buffer before reading, not after Previously, with high input pressure where full "sizeof(input_buf) - 1" bytes could be read, all content would be dropped right after reading, and it could happen with every single read. Example: > # ./ttyplot < /dev/random --- ttyplot.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ttyplot.c b/ttyplot.c index 638c766..d97699f 100644 --- a/ttyplot.c +++ b/ttyplot.c @@ -514,19 +514,18 @@ int main(int argc, char *argv[]) { // Read as much from stdin as we can (first read after select is non-blocking) if (stdin_can_be_read_without_blocking) { + // Last resort: truncate existing input if input line ever is + // too long + if (input_len >= sizeof(input_buf) - 1 /* terminator */) { + input_len = 0; + } + char * const read_target = input_buf + input_len; const size_t max_bytes_to_read = sizeof(input_buf) - (input_len + 1 /* terminator */); const ssize_t bytes_read_or_error = read(STDIN_FILENO, read_target, max_bytes_to_read); if (bytes_read_or_error > 0) { input_len += bytes_read_or_error; - - // Last resort: truncate existing input if input line ever is - // too long - if (input_len >= sizeof(input_buf) - 1) { - input_len = 0; - } - assert(input_len < sizeof(input_buf)); input_buf[input_len] = '\0'; } else {