From e3d10c994d3abf47706ee2fb9b09cde2c3300208 Mon Sep 17 00:00:00 2001 From: Terry Golubiewski Date: Sun, 8 Nov 2026 08:10:13 -0800 Subject: [PATCH] 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. --- include/fmt/core.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/fmt/core.h b/include/fmt/core.h index 1bcf6442..0d675baf 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -462,6 +462,7 @@ constexpr FMT_ALWAYS_INLINE auto narrow(const char* s) -> const char* { template 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;