Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix over-eager buffer truncation (regression from #98) #129

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions ttyplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down