Fix null pointers passed to memcmp/memcpy in skip_list #65

Merged
andrew merged 1 commits from weaselbot/conflict-set:weaselbot/issue-64 into main 2026-07-13 21:55:54 +00:00
Showing only changes of commit 2231c093df - Show all commits
+8 -3
View File
@@ -38,6 +38,9 @@ std::span<const uint8_t> keyAfter(Arena &arena, std::span<const uint8_t> key) {
std::span<const uint8_t> copyToArena(Arena &arena, std::span<const uint8_t> copyToArena(Arena &arena,
std::span<const uint8_t> key) { std::span<const uint8_t> key) {
if (key.size() == 0) {
return std::span<const uint8_t>();
}
auto result = std::span<uint8_t>(new (arena) uint8_t[key.size()], key.size()); auto result = std::span<uint8_t>(new (arena) uint8_t[key.size()], key.size());
memcpy(result.data(), key.data(), key.size()); memcpy(result.data(), key.data(), key.size());
return result; return result;
@@ -97,7 +100,7 @@ force_inline bool getCharacter(const KeyInfo &ki, int character,
bool operator<(const KeyInfo &lhs, const KeyInfo &rhs) { bool operator<(const KeyInfo &lhs, const KeyInfo &rhs) {
int i = std::min(lhs.key.size(), rhs.key.size()); int i = std::min(lhs.key.size(), rhs.key.size());
int c = memcmp(lhs.key.data(), rhs.key.data(), i); int c = i > 0 ? memcmp(lhs.key.data(), rhs.key.data(), i) : 0;
andrew marked this conversation as resolved Outdated
Outdated
Review

If one of them is non-empty, this could compare them incorrectly

If one of them is non-empty, this could compare them incorrectly
if (c != 0) if (c != 0)
return c < 0; return c < 0;
@@ -287,7 +290,8 @@ private:
static force_inline bool less(const uint8_t *a, int aLen, const uint8_t *b, static force_inline bool less(const uint8_t *a, int aLen, const uint8_t *b,
int bLen) { int bLen) {
int c = memcmp(a, b, std::min(aLen, bLen)); int n = std::min(aLen, bLen);
int c = n > 0 ? memcmp(a, b, n) : 0;
if (c < 0) if (c < 0)
return true; return true;
if (c > 0) if (c > 0)
@@ -754,7 +758,8 @@ struct __attribute__((visibility("hidden"))) ConflictSet::Impl {
void addWrites(const ConflictSet::WriteRange *writes, int count, void addWrites(const ConflictSet::WriteRange *writes, int count,
int64_t writeVersion) { int64_t writeVersion) {
auto points = std::vector<KeyInfo>(count * 2); auto points = std::vector<KeyInfo>();
points.reserve(count * 2);
Arena arena; Arena arena;
for (int r = 0; r < count; r++) { for (int r = 0; r < count; r++) {