Skip to content

Commit 2ea4251

Browse files
committed
fix: replaced an assert to an exception in CSkipSet inside Add
assert can be triggered if Add() is called without validation of SkipSet conditions
1 parent 5d29b06 commit 2ea4251

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/util/skip_set.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
#include <util/skip_set.h>
66

77
#include <logging.h>
8+
#include <stdexcept>
89

910
bool CSkipSet::Add(uint64_t value)
1011
{
11-
assert(!Contains(value));
12+
if (Contains(value)) {
13+
throw std::runtime_error(strprintf("%s: trying to add an element that can't be added", __func__));
14+
}
1215

1316
if (auto it = skipped.find(value); it != skipped.end()) {
1417
skipped.erase(it);

src/util/skip_set.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <serialize.h>
66
#include <unordered_set>
77

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

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

0 commit comments

Comments
 (0)