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 <bhalevy@scylladb.com> Test: unit(dev) Branches: all Message-Id: <20200709123453.955569-1-bhalevy@scylladb.com>
This commit is contained in:
5
bytes.hh
5
bytes.hh
@@ -84,9 +84,12 @@ struct appending_hash<bytes_view> {
|
||||
};
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user