fix: cast char to unsigned char before passing to isspace to prevent UB

This commit is contained in:
pro-laxmi authored and Carter Li committed 2026-11-14 12:35:08 +08:00
1 parent 6a82126aa9
commit c6c8bffbab
4 files changed
+5 -3

No files matched your search

+1 -1
View File
@@ -567,7 +567,7 @@ static inline bool ffStrbufInitJsonVal(FFstrbuf* strbuf, yyjson_val* jsonVal) {
// Not `pure`: the caller-supplied `fn` may have side effects
[[gnu::nonnull(1, 2), nodiscard]] static inline bool ffStrbufEndsWithFn(const FFstrbuf* strbuf, int (*const fn)(int)) {
return strbuf->length == 0 ? false : fn(strbuf->chars[strbuf->length - 1]);
return strbuf->length == 0 ? false : fn((unsigned char)strbuf->chars[strbuf->length - 1]);
}
[[gnu::nonnull(1, 2), gnu::pure, nodiscard]] static inline bool ffStrbufEndsWith(const FFstrbuf* strbuf, const FFstrbuf* end) {
+1 -1
View File
@@ -323,7 +323,7 @@ void ffStrbufTrimLeftSpace(FFstrbuf* strbuf) {
}
uint32_t index = 0;
while (index < strbuf->length && isspace(strbuf->chars[index])) {
while (index < strbuf->length && isspace((unsigned char)strbuf->chars[index])) {
++index;
}
+1 -1
View File
@@ -28,7 +28,7 @@ static inline bool ffStrSet(const char* str) {
return false;
}
while (isspace(*str)) {
while (isspace((unsigned char)*str)) {
str++;
}
+2
View File
@@ -26,6 +26,8 @@ int main(void) {
VERIFY(ffStrSet("a"));
VERIFY(ffStrSet(" a"));
VERIFY(ffStrSet("a "));
VERIFY(ffStrSet("\x80"));
VERIFY(ffStrSet("\xff"));
}
{