From 283b6bd7619d7c3f3c98dbeccaeb27e70b9ea38f Mon Sep 17 00:00:00 2001 From: Masatake Daimon Date: Fri, 7 Nov 2025 01:42:00 +0900 Subject: utils: Correct ctype(3) usage According to POSIX.1 the argument of ctype(3) functions, including toupper(3), has to either be EOF or a value representable as an unsigned char. If the argument has any other value, the behavior is undefined. This means values of signed char must first be cast to unsigned char, otherwise they will be outside the range of allowed values. glibc attempts to avoid the undefined behavior by defining the functions to work for all integer inputs representable by either an unsigned char or a signed char. NetBSD libc does not, and causes a segfault if the value is invalid. --- src/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 2c5c15d..33b76d6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -220,7 +220,7 @@ char *string_toupper(char* str) char *res = strdup(str); size_t i; for (i = 0; i < strlen(res); i++) { - res[i] = toupper(res[i]); + res[i] = toupper((unsigned char)res[i]); } return res; } -- cgit v1.1-32-gdbae