scc

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

commit 07426bb20d43eaaf7c931656e3b9cf72e9239360
parent 5abfa5527c796fb26a1e1c1074a603aa64af27bb
Author: Quentin Rameau <quinq@fifth.space>
Date:   Tue, 12 Sep 2017 11:21:23 +0200

[libc] ctype: don't cast parameter to unsigned char

Casting values hides some possible bugs, instead apply friendly
undefined behaviour and let the developer fix his code.

Diffstat:
Mlib/c/include/ctype.h | 26+++++++++++++-------------
Mlib/c/src/ctype.c | 4+++-
Mlib/c/src/isalnum.c | 2+-
Mlib/c/src/isalpha.c | 2+-
Mlib/c/src/iscntrl.c | 2+-
Mlib/c/src/isdigit.c | 2+-
Mlib/c/src/isgraph.c | 2+-
Mlib/c/src/islower.c | 2+-
Mlib/c/src/isprint.c | 2+-
Mlib/c/src/ispunct.c | 2+-
Mlib/c/src/isspace.c | 2+-
Mlib/c/src/isupper.c | 2+-
Mlib/c/src/isxdigit.c | 2+-
13 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/lib/c/include/ctype.h b/lib/c/include/ctype.h @@ -29,19 +29,19 @@ extern int toupper(int c); extern unsigned char __ctype[]; -#define isalnum(c) (__ctype[(unsigned char) c] & (_U|_L|_D)) -#define isalpha(c) (__ctype[(unsigned char) c] & (_U|_L)) -#define iscntrl(c) (__ctype[(unsigned char) c] & (_C)) -#define isdigit(c) (__ctype[(unsigned char) c] & (_D)) -#define isgraph(c) (__ctype[(unsigned char) c] & (_P|_U|_L|_D)) -#define islower(c) (__ctype[(unsigned char) c] & (_L)) -#define isprint(c) (__ctype[(unsigned char) c] & (_P|_U|_L|_D|_SP)) -#define ispunct(c) (__ctype[(unsigned char) c] & (_P)) -#define isspace(c) (__ctype[(unsigned char) c] & (_S)) -#define isupper(c) (__ctype[(unsigned char) c] & (_U)) -#define isxdigit(c) (__ctype[(unsigned char) c] & (_D|_X)) - -#define isascii(c) (((unsigned) c)<=0x7f) +#define isalnum(c) ((__ctype+1)[(c)] & (_U|_L|_D)) +#define isalpha(c) ((__ctype+1)[(c)] & (_U|_L)) +#define iscntrl(c) ((__ctype+1)[(c)] & (_C)) +#define isdigit(c) ((__ctype+1)[(c)] & (_D)) +#define isgraph(c) ((__ctype+1)[(c)] & (_P|_U|_L|_D)) +#define islower(c) ((__ctype+1)[(c)] & (_L)) +#define isprint(c) ((__ctype+1)[(c)] & (_P|_U|_L|_D|_SP)) +#define ispunct(c) ((__ctype+1)[(c)] & (_P)) +#define isspace(c) ((__ctype+1)[(c)] & (_S)) +#define isupper(c) ((__ctype+1)[(c)] & (_U)) +#define isxdigit(c) ((__ctype+1)[(c)] & (_D|_X)) + +#define isascii(c) ((unsigned)(c)<=0x7f) #endif diff --git a/lib/c/src/ctype.c b/lib/c/src/ctype.c @@ -2,7 +2,9 @@ #include <ctype.h> #undef ctype -unsigned char __ctype[255] = { +/* __ctype is shifted by one to match EOF */ +unsigned char __ctype[256] = { + 0, /* EOF */ _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */ _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */ _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */ diff --git a/lib/c/src/isalnum.c b/lib/c/src/isalnum.c @@ -5,5 +5,5 @@ int isalnum(int c) { - return __ctype[(unsigned char) c] & (_U|_L|_D); + return (__ctype+1)[c] & (_U|_L|_D); } diff --git a/lib/c/src/isalpha.c b/lib/c/src/isalpha.c @@ -5,5 +5,5 @@ int isalpha(int c) { - return __ctype[(unsigned char) c] & (_U|_L); + return (__ctype+1)[c] & (_U|_L); } diff --git a/lib/c/src/iscntrl.c b/lib/c/src/iscntrl.c @@ -5,5 +5,5 @@ int iscntrl(int c) { - return __ctype[(unsigned char) c] & (_C); + return (__ctype+1)[c] & (_C); } diff --git a/lib/c/src/isdigit.c b/lib/c/src/isdigit.c @@ -5,5 +5,5 @@ int isdigit(int c) { - return __ctype[(unsigned char) c] & (_D); + return (__ctype+1)[c] & (_D); } diff --git a/lib/c/src/isgraph.c b/lib/c/src/isgraph.c @@ -5,5 +5,5 @@ int isgraph(int c) { - return __ctype[(unsigned char) c] & (_P|_U|_L|_D); + return (__ctype+1)[c] & (_P|_U|_L|_D); } diff --git a/lib/c/src/islower.c b/lib/c/src/islower.c @@ -5,5 +5,5 @@ int islower(int c) { - return __ctype[(unsigned char) c] & _L; + return (__ctype+1)[c] & _L; } diff --git a/lib/c/src/isprint.c b/lib/c/src/isprint.c @@ -5,5 +5,5 @@ int isprint(int c) { - return __ctype[(unsigned char) c] & (_P|_U|_L|_D|_SP); + return (__ctype+1)[c] & (_P|_U|_L|_D|_SP); } diff --git a/lib/c/src/ispunct.c b/lib/c/src/ispunct.c @@ -5,5 +5,5 @@ int ispunct(int c) { - return __ctype[(unsigned char) c] & (_P); + return (__ctype+1)[c] & (_P); } diff --git a/lib/c/src/isspace.c b/lib/c/src/isspace.c @@ -5,5 +5,5 @@ int isspace(int c) { - return __ctype[(unsigned char) c] & _S; + return (__ctype+1)[c] & _S; } diff --git a/lib/c/src/isupper.c b/lib/c/src/isupper.c @@ -5,5 +5,5 @@ int isupper(int c) { - return __ctype[(unsigned char) c] & _U; + return (__ctype+1)[c] & _U; } diff --git a/lib/c/src/isxdigit.c b/lib/c/src/isxdigit.c @@ -5,5 +5,5 @@ int isxdigit(int c) { - return __ctype[(unsigned char) c] & (_D|_X); + return (__ctype+1)[c] & (_D|_X); }