This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 85cb00ec4d35b778b1d6418b34a55b2190b6f6f1 Author: fangxinyong <[email protected]> AuthorDate: Mon Sep 28 17:40:42 2026 +0800 libc/string: Cast characters to unsigned char in strncmp() and toupper(). When plain char is signed, bytes >= 0x80 are sign extended to a negative int. strncmp() (BSD variant) then returns the wrong sign for such bytes, although C11 7.24.4 requires the comparison to be done as unsigned char. The same applies to toupper() in strcasecmp(), strncasecmp() and strcasestr(), whose argument must be representable as unsigned char. Cast the characters before using them. Signed-off-by: fangxinyong <[email protected]> Signed-off-by: ganjing <[email protected]> --- libs/libc/string/lib_bsdstrncmp.c | 2 +- libs/libc/string/lib_strcasecmp.c | 5 +++-- libs/libc/string/lib_strcasestr.c | 9 +++++---- libs/libc/string/lib_strncasecmp.c | 5 +++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/libs/libc/string/lib_bsdstrncmp.c b/libs/libc/string/lib_bsdstrncmp.c index f341f86f3ef..21b307144f6 100644 --- a/libs/libc/string/lib_bsdstrncmp.c +++ b/libs/libc/string/lib_bsdstrncmp.c @@ -129,6 +129,6 @@ int strncmp(FAR const char *cs, FAR const char *ct, size_t nb) ct++; } - return *cs - *ct; + return (unsigned char)*cs - (unsigned char)*ct; } #endif diff --git a/libs/libc/string/lib_strcasecmp.c b/libs/libc/string/lib_strcasecmp.c index 97f6cd79121..3e9f9a87924 100644 --- a/libs/libc/string/lib_strcasecmp.c +++ b/libs/libc/string/lib_strcasecmp.c @@ -43,8 +43,9 @@ int strcasecmp(FAR const char *cs, FAR const char *ct) for (; ; ) { - if ((result = toupper(*cs) - toupper(*ct)) != 0 || - *cs == '\0') + result = toupper((unsigned char)*cs) - + toupper((unsigned char)*ct); + if (result != 0 || *cs == '\0') { break; } diff --git a/libs/libc/string/lib_strcasestr.c b/libs/libc/string/lib_strcasestr.c index 05a9897fd63..a30377617dd 100644 --- a/libs/libc/string/lib_strcasestr.c +++ b/libs/libc/string/lib_strcasestr.c @@ -42,7 +42,7 @@ static FAR char *strcasechr(FAR const char *s, int uc) for (; *s != '\0'; s++) { ch = *s; - if (toupper(ch) == uc) + if (toupper((unsigned char)ch) == uc) { return (FAR char *)s; } @@ -59,6 +59,7 @@ FAR char *strcasestr(FAR const char *str, FAR const char *substr) { FAR const char *candidate; /* Candidate in str with matching start character */ char ch; /* First character of the substring */ + int uc; /* Upper case of the first character */ size_t len; /* The length of the substring */ /* Special case the empty substring */ @@ -78,15 +79,15 @@ FAR char *strcasestr(FAR const char *str, FAR const char *substr) /* Search for the substring */ candidate = str; - ch = toupper(ch); + uc = toupper((unsigned char)ch); for (; ; ) { /* strcasechr() will return a pointer to the next occurrence of the - * character ch in the string (ignoring case) + * character uc in the string (ignoring case) */ - candidate = strcasechr(candidate, ch); + candidate = strcasechr(candidate, uc); if (!candidate || strlen(candidate) < len) { /* First character of the substring does not appear in the string diff --git a/libs/libc/string/lib_strncasecmp.c b/libs/libc/string/lib_strncasecmp.c index 189e37448ad..e46e6bf95ad 100644 --- a/libs/libc/string/lib_strncasecmp.c +++ b/libs/libc/string/lib_strncasecmp.c @@ -44,8 +44,9 @@ int strncasecmp(FAR const char *cs, FAR const char *ct, size_t nb) for (; nb > 0; nb--) { - if ((result = toupper(*cs) - toupper(*ct)) != 0 || - *cs == '\0') + result = toupper((unsigned char)*cs) - + toupper((unsigned char)*ct); + if (result != 0 || *cs == '\0') { break; }
