]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
string-util: explicitly cast character to unsigned
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 8 May 2021 05:56:31 +0000 (14:56 +0900)
committerLennart Poettering <lennart@poettering.net>
Sat, 8 May 2021 12:24:51 +0000 (14:24 +0200)
This also adds comment why we cast to unsigned.

Follow-up for 7971f9030ae4bebe0d4a6845ed31584f8ab18103.

Addresses the comment https://github.com/systemd/systemd/pull/19544#discussion_r628472794.

src/basic/string-util.h

index 3884777792cefb17ff72b75d29db9a40f06c6f7d..4ef3254dc5dc025701093b2726561e2bd7c64243 100644 (file)
@@ -130,7 +130,12 @@ static inline bool _pure_ in_charset(const char *s, const char* charset) {
 }
 
 static inline bool char_is_cc(char p) {
-        return (uint8_t) p < ' ' || p == 127;
+        /* char is unsigned on some architectures, e.g. aarch64. So, compiler may warn the condition
+         * p >= 0 is always true. See #19543. Hence, let's cast to unsigned before the comparison. Note
+         * that the cast in the right hand side is redundant, as according to the C standard, compilers
+         * automatically cast a signed value to unsigned when comparing with an unsigned variable. Just
+         * for safety and readability. */
+        return (uint8_t) p < (uint8_t) ' ' || p == 127;
 }
 bool string_has_cc(const char *p, const char *ok) _pure_;