Skip to content

Commit

Permalink
fix: replaced an assert to an exception in CSkipSet inside Add
Browse files Browse the repository at this point in the history
assert can be triggered if Add() is called without validation of SkipSet conditions
  • Loading branch information
knst committed Jul 14, 2023
1 parent 5d29b06 commit 2ea4251
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/util/skip_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#include <util/skip_set.h>

#include <logging.h>
#include <stdexcept>

bool CSkipSet::Add(uint64_t value)
{
assert(!Contains(value));
if (Contains(value)) {
throw std::runtime_error(strprintf("%s: trying to add an element that can't be added", __func__));
}

if (auto it = skipped.find(value); it != skipped.end()) {
skipped.erase(it);
Expand Down
8 changes: 6 additions & 2 deletions src/util/skip_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <serialize.h>
#include <unordered_set>

// This datastructure keeps efficiently all indexes and have a strict limit for used memory
// This data structure keeps efficiently all indexes and have a strict limit for used memory
// So far as CCreditPool is built only in direction from parent block to child
// there's no need to remove elements from CSkipSet ever, only add them
class CSkipSet {
Expand All @@ -19,8 +19,12 @@ class CSkipSet {
{}

/**
* adding value that already exist in CKipSet will cause `assert`.
* `Add` returns true if element has been added correctly and false if
* capacity is depleted.
*
* `Add` should not be called if the element has been already added.
* Use `Contains` to check if the element is here
* Adding existing value will cause an exception
*/
[[nodiscard]] bool Add(uint64_t value);

Expand Down

0 comments on commit 2ea4251

Please sign in to comment.