fix undefined behaviour of using isspace ctype function

cast all ctype(3) functions argument to (unsigned char) to avoid UB

POSIX says:
"The c argument is an int, the value of which the application shall ensure is a
character representable as an unsigned char or equal to the value of the macro
EOF. If the argument has any other value, the behavior is undefined."

Many libc cast implicitly the value, but NetBSD for example does not, which is
probably the correct thing to interpret it.
This commit is contained in:
Hiltjo Posthuma 2021-05-06 01:09:21 +02:00
parent df4c061136
commit d9bda20849
2 changed files with 3 additions and 3 deletions

2
sic.c
View File

@ -74,7 +74,7 @@ parsein(char *s) {
return; return;
} }
c = *++s; c = *++s;
if(c != '\0' && isspace(s[1])) { if(c != '\0' && isspace((unsigned char)s[1])) {
p = s + 2; p = s + 2;
switch(c) { switch(c) {
case 'j': case 'j':

4
util.c
View File

@ -42,7 +42,7 @@ dial(char *host, char *port) {
static char * static char *
eat(char *s, int (*p)(int), int r) { eat(char *s, int (*p)(int), int r) {
while(*s != '\0' && p(*s) == r) while(*s != '\0' && p((unsigned char)*s) == r)
s++; s++;
return s; return s;
} }
@ -61,7 +61,7 @@ trim(char *s) {
char *e; char *e;
e = s + strlen(s) - 1; e = s + strlen(s) - 1;
while(e > s && isspace(*e)) while(e > s && isspace((unsigned char)*e))
e--; e--;
*(e + 1) = '\0'; *(e + 1) = '\0';
} }