mirror of
https://github.com/fmtlib/fmt.git
synced 2026-11-09 14:50:24 +08:00
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:
parent
ded661bf35
commit
e3d10c994d
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user