Skip to content

Commit 3d56ace

Browse files
yuwatapoettering
authored andcommitted
string-util: explicitly cast character to unsigned
This also adds comment why we cast to unsigned. Follow-up for 7971f90. Addresses the comment systemd#19544 (comment).
1 parent 11c38d3 commit 3d56ace

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/basic/string-util.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ static inline bool _pure_ in_charset(const char *s, const char* charset) {
130130
}
131131

132132
static inline bool char_is_cc(char p) {
133-
return (uint8_t) p < ' ' || p == 127;
133+
/* char is unsigned on some architectures, e.g. aarch64. So, compiler may warn the condition
134+
* p >= 0 is always true. See #19543. Hence, let's cast to unsigned before the comparison. Note
135+
* that the cast in the right hand side is redundant, as according to the C standard, compilers
136+
* automatically cast a signed value to unsigned when comparing with an unsigned variable. Just
137+
* for safety and readability. */
138+
return (uint8_t) p < (uint8_t) ' ' || p == 127;
134139
}
135140
bool string_has_cc(const char *p, const char *ok) _pure_;
136141

0 commit comments

Comments
 (0)