Skip to content

Commit

Permalink
Protect against undefined behavior from inputs like "NaN"
Browse files Browse the repository at this point in the history
Reproducer was (against commit 07ce2f9 on master):
> # make clean all {C,LD}FLAGS='-fsanitize=undefined -fno-sanitize-recover=all' \
>     && ./ttyplot <<<NaN |& tee /tmp/foo \
>     && reset \
>     && grep -oE ttyplot\.c.+ /tmp/foo

With GCC you would get:
> ttyplot.c:129:9: runtime error: signed integer overflow: 0 - -2147483648 cannot be represented in type 'int'

With Clang you would get:
> ttyplot.c:144:58: runtime error: nan is outside the range of representable values of type 'int'
  • Loading branch information
hartwork committed Nov 23, 2023
1 parent c150198 commit c686739
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ttyplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <assert.h>
#include <ctype.h> // isspace
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -529,6 +530,11 @@ int main(int argc, char *argv[]) {
else
n=0;

if (! isfinite(d1))
d1 = 0.0;
if (two && ! isfinite(d2))
d2 = 0.0;

values1[n] = d1;
values2[n] = d2;

Expand Down

0 comments on commit c686739

Please sign in to comment.