scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 5c4275a3d6c5c93b533dee468b2314eeb74c58a3
parent 88d8273a00a45b5fc2584513f360a3d63de8cb55
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 12 Sep 2017 08:12:37 +0100

[cc1] Add warning about invalid input character

Ctype functions have undefined behaviour out of the range of unsigned char,
and they are only meanful in the character set defined by the current locale,
that in our case is the "C" locale. The "C" locale warranties that all the
characters are representable in the range of positive chars.

The value returned by getchar() is always a number in the range of unsigned
char or EOF, so the use of ctype functions is safe in readchar() and it
will generate the expected behaviour. After that point (and after showing
the proper warning) the value is stored in a char array, and using that
value from the char array will produce a sign extension if char is signed
and the value is negative, and the resultant value will be an invalid value
for ctype functions, and the result will depend of the implementation of
the libraries used to compile scc. The shame of UB is yours!!!

Diffstat:
cc1/lex.c | 4++++
1 file changed, 4 insertions(+), 0 deletions(-)

diff --git a/cc1/lex.c b/cc1/lex.c @@ -198,6 +198,10 @@ repeat: case '\n': newline(); break; + default: + if (!isprint(c) && !ispunct(c)) + warn("invalid input character. The shame of UB is yours"); + break; } return c;