Skip to content

Commit

Permalink
ttyplot.c: Be robust towards input containing null bytes
Browse files Browse the repository at this point in the history
> # { python3 -c 'print(chr(0))'; seq 20; } | ./ttyplot
  • Loading branch information
hartwork committed Nov 28, 2023
1 parent 4ce426f commit 37174fa
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ttyplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,14 @@ int main(int argc, char *argv[]) {
const ssize_t bytes_read_or_error = read(STDIN_FILENO, read_target, max_bytes_to_read);

if (bytes_read_or_error > 0) {
// The data we read could contain null bytes, so we replace those
// by one of the supported delimiters to not lose all input coming after.
for (size_t i = input_len; i < input_len + bytes_read_or_error; i++) {
if (input_buf[i] == '\0') {
input_buf[i] = ' ';
}
}

input_len += bytes_read_or_error;

// Last resort: truncate existing input if input line ever is
Expand Down

0 comments on commit 37174fa

Please sign in to comment.