From 5c5a526d0abe22cb7fc30707bd14f09d8b5d1f9c Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Fri, 7 Sep 2018 19:00:57 +0200 Subject: fix many undefined behaviour in usage of ctype functions - cast all ctype(3) function 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 does not, which is probably the correct thing to interpret it. - no need to cast for putchar + rename some fputc(..., stdout) to putchar POSIX says: "The fputc() function shall write the byte specified by c (converted to an unsigned char) to the output stream pointed to by stream [...]" Major thanks to Leonardo Taccari for reporting and testing it on NetBSD! --- util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index abe30a3..e74983f 100644 --- a/util.c +++ b/util.c @@ -31,7 +31,7 @@ parseuri(const char *s, struct uri *u, int rel) p += 2; /* skip "//" */ } else { /* protocol part */ - for (p = s; *p && (isalpha((int)*p) || isdigit((int)*p) || + for (p = s; *p && (isalpha((unsigned char)*p) || isdigit((unsigned char)*p) || *p == '+' || *p == '-' || *p == '.'); p++) ; if (!strncmp(p, "://", 3)) { @@ -101,7 +101,7 @@ encodeuri(char *buf, size_t bufsiz, const char *s) for (i = 0, b = 0; s[i]; i++) { if ((int)s[i] == ' ' || (unsigned char)s[i] > 127 || - iscntrl((int)s[i])) { + iscntrl((unsigned char)s[i])) { if (b + 3 >= bufsiz) return -1; buf[b++] = '%'; -- cgit v1.2.3