Merge pull request #117219 from Ivorforce/span-equality-nullptr

Fix `are_spans_equal` for `p_size = 0` when pointers may be invalid
This commit is contained in:
Rémi Verschelde 2026-03-09 11:49:50 +01:00
commit 74de2ec047
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -38,7 +38,9 @@ bool are_spans_equal(const LHS *p_lhs, const RHS *p_rhs, size_t p_size) {
if constexpr (std::is_same_v<LHS, RHS> && std::is_fundamental_v<LHS>) {
// Optimize trivial type comparison.
// is_trivially_equality_comparable would help, but it doesn't exist.
return memcmp(p_lhs, p_rhs, p_size * sizeof(LHS)) == 0;
// memcmp requires pointer argument to be valid even on size = 0 (C11 §7.24.1(2)).
// Span allows ptr arguments to be invalid as long as size = 0, so only call when p_size > 0.
return p_size == 0 || memcmp(p_lhs, p_rhs, p_size * sizeof(LHS)) == 0;
} else {
// Normal case: Need to iterate the array manually.
for (size_t j = 0; j < p_size; j++) {