fix: avoid null pointers in zero-length string comparisons (#5014)

Return zero from detail::compare when n == 0. Sanitizer checking reported
null pointers being passed to memcmp when comparing empty string views,
even though the comparison length was zero.
This commit is contained in:
Terry Golubiewski 2026-11-08 08:10:13 -08:00 committed by Victor Zverovich
parent ded661bf35
commit e3d10c994d

View File

@ -462,6 +462,7 @@ constexpr FMT_ALWAYS_INLINE auto narrow(const char* s) -> const char* {
template <typename Char>
FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, size_t n) -> int {
if (n == 0) return 0;
if (!is_constant_evaluated() && sizeof(Char) == 1) return memcmp(s1, s2, n);
for (; n != 0; ++s1, ++s2, --n) {
if (*s1 < *s2) return -1;