From 1ea759dbe2db5e7a737a4956c38d137aca1a2f90 Mon Sep 17 00:00:00 2001 From: Alexander Lopez Date: Sat, 4 Apr 2026 16:00:40 -0700 Subject: [PATCH 1/2] fix unsafe cast for many sanitizers --- source/str_view.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/source/str_view.c b/source/str_view.c index c3e8875..de9f43e 100644 --- a/source/str_view.c +++ b/source/str_view.c @@ -817,9 +817,18 @@ char_compare(char const a, char const b) { /* This is section is modeled after the musl string.h library. However, using SV_Str_view that may not be null terminated requires modifications. */ -#define BITOP(a, b, op) \ - ((a)[(size_t)(b) / (8 * sizeof *(a))] op(size_t)( \ - 1 << ((size_t)(b) % (8 * sizeof *(a))))) +static inline bool +bitset_set(size_t *const bitset, size_t const char_as_size_t) { + return bitset[char_as_size_t / (8 * sizeof(*bitset))] + |= (size_t)(1 << (char_as_size_t % (8 * sizeof(*bitset)))); +} + +static inline bool +bitset_test(size_t const *bitset, size_t const char_as_size_t) { + return (bitset[char_as_size_t / (8 * sizeof(*bitset))] + & (size_t)(1 << (char_as_size_t % (8 * sizeof(*bitset))))) + != 0; +} /* This is dangerous. Do not use this under normal circumstances. This is an internal helper for the backwards two way string @@ -856,10 +865,11 @@ view_span_in_set_complement_length(size_t const str_size, return (size_t)(a - str); } for (size_t i = 0; - i < set_size && BITOP(byteset, *(unsigned char *)set, |=); + i < set_size && bitset_set(byteset, *(unsigned char *)set); ++set, ++i) {} - for (size_t i = 0; i < str_size && !BITOP(byteset, *(unsigned char *)a, &); - ++a, ++i) {} + for (size_t i = 0; + i < str_size && !bitset_test(byteset, *(unsigned char *)a); ++a, ++i) { + } return (size_t)(a - str); } @@ -884,10 +894,10 @@ view_span_in_set_length(size_t const str_size, return (size_t)(a - str); } for (size_t i = 0; - i < set_size && BITOP(byteset, *(unsigned char *)set, |=); + i < set_size && bitset_set(byteset, *(unsigned char *)set); ++set, ++i) {} - for (size_t i = 0; i < str_size && BITOP(byteset, *(unsigned char *)a, &); - ++a, ++i) {} + for (size_t i = 0; + i < str_size && bitset_test(byteset, *(unsigned char *)a); ++a, ++i) {} return (size_t)(a - str); } From 3eee6e434f4d77d41a707cce93c8a1d8a049528d Mon Sep 17 00:00:00 2001 From: Alexander Lopez Date: Sat, 4 Apr 2026 16:03:23 -0700 Subject: [PATCH 2/2] make bool explicit --- source/str_view.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/str_view.c b/source/str_view.c index de9f43e..a6207b1 100644 --- a/source/str_view.c +++ b/source/str_view.c @@ -819,8 +819,9 @@ char_compare(char const a, char const b) { static inline bool bitset_set(size_t *const bitset, size_t const char_as_size_t) { - return bitset[char_as_size_t / (8 * sizeof(*bitset))] - |= (size_t)(1 << (char_as_size_t % (8 * sizeof(*bitset)))); + return (bitset[char_as_size_t / (8 * sizeof(*bitset))] + |= (size_t)(1 << (char_as_size_t % (8 * sizeof(*bitset))))) + != 0; } static inline bool