From ec77777bdae402dfd32911507aceca49eb84cd34 Mon Sep 17 00:00:00 2001 From: Benny Halevy Date: Thu, 9 Jul 2020 15:34:53 +0300 Subject: [PATCH] bytes: compare_unsigned: do not pass nullptr to memcmp If any of the compared bytes_view's is empty consider the empty prefix is same and proceed to compare the size of the suffix. A similar issue exists in legacy_compound_view::tri_comparator::operator(). It too must not pass nullptr to memcmp if any of the compared byte_view's is empty. Fixes #6797 Refs #6814 Signed-off-by: Benny Halevy Test: unit(dev) Branches: all Message-Id: <20200709123453.955569-1-bhalevy@scylladb.com> --- bytes.hh | 5 ++++- compound_compat.hh | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/bytes.hh b/bytes.hh index 9d1a0689b9..2999abde01 100644 --- a/bytes.hh +++ b/bytes.hh @@ -84,9 +84,12 @@ struct appending_hash { }; inline int32_t compare_unsigned(bytes_view v1, bytes_view v2) { - auto n = memcmp(v1.begin(), v2.begin(), std::min(v1.size(), v2.size())); + auto size = std::min(v1.size(), v2.size()); + if (size) { + auto n = memcmp(v1.begin(), v2.begin(), size); if (n) { return n; } + } return (int32_t) (v1.size() - v2.size()); } diff --git a/compound_compat.hh b/compound_compat.hh index e8e8b03f1d..8b825e30d3 100644 --- a/compound_compat.hh +++ b/compound_compat.hh @@ -148,8 +148,8 @@ public: _type.begin(k1), _type.end(k1), _type.begin(k2), _type.end(k2), [] (const bytes_view& c1, const bytes_view& c2) -> int { - if (c1.size() != c2.size()) { - return c1.size() < c2.size() ? -1 : 1; + if (c1.size() != c2.size() || !c1.size()) { + return c1.size() < c2.size() ? -1 : c1.size() ? 1 : 0; } return memcmp(c1.begin(), c2.begin(), c1.size()); });