]> git.armaanb.net Git - sic.git/commitdiff
fix undefined behaviour of using isspace ctype function
authorHiltjo Posthuma <hiltjo@codemadness.org>
Wed, 5 May 2021 23:09:21 +0000 (01:09 +0200)
committerHiltjo Posthuma <hiltjo@codemadness.org>
Wed, 5 May 2021 23:09:21 +0000 (01:09 +0200)
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.

sic.c
util.c

diff --git a/sic.c b/sic.c
index 774f357aff161655e710fd60d26e79be871d1d24..c1e428effa57a8675c123f100d122a1971a210a2 100644 (file)
--- a/sic.c
+++ b/sic.c
@@ -74,7 +74,7 @@ parsein(char *s) {
                return;
        }
        c = *++s;
-       if(c != '\0' && isspace(s[1])) {
+       if(c != '\0' && isspace((unsigned char)s[1])) {
                p = s + 2;
                switch(c) {
                case 'j':
diff --git a/util.c b/util.c
index 25678120422eea5ec0782e9040fdd5ad24b9ff45..0869cca08401a08f53878653f3a37d5bf0d6a12f 100644 (file)
--- a/util.c
+++ b/util.c
@@ -42,7 +42,7 @@ dial(char *host, char *port) {
 
 static char *
 eat(char *s, int (*p)(int), int r) {
-       while(*s != '\0' && p(*s) == r)
+       while(*s != '\0' && p((unsigned char)*s) == r)
                s++;
        return s;
 }
@@ -61,7 +61,7 @@ trim(char *s) {
        char *e;
 
        e = s + strlen(s) - 1;
-       while(e > s && isspace(*e))
+       while(e > s && isspace((unsigned char)*e))
                e--;
        *(e + 1) = '\0';
 }