Skip to content

Commit 2ab2249

Browse files
hebastosipa
andcommitted
Fix UB in benchmark
From https://en.cppreference.com/w/c/language/operator_arithmetic.html: > The behavior is undefined if rhs is negative or is greater or equal > the number of bits in the promoted lhs. Co-authored-by: Pieter Wuille <[email protected]>
1 parent f74b7e2 commit 2ab2249

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/bench.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "../include/minisketch.h"
88
#include <string.h>
9+
#include <limits>
910
#include <memory>
1011
#include <vector>
1112
#include <chrono>
@@ -42,7 +43,8 @@ int main(int argc, char** argv) {
4243
std::vector<minisketch*> states;
4344
std::vector<uint64_t> roots(2 * syndromes);
4445
std::random_device rng;
45-
std::uniform_int_distribution<uint64_t> dist(1, (uint64_t(1) << bits) - 1);
46+
auto upper_bound = std::numeric_limits<uint64_t>::max() >> (64 - bits);
47+
std::uniform_int_distribution<uint64_t> dist(1, upper_bound);
4648
states.resize(iters);
4749
std::vector<double> benches;
4850
benches.reserve(iters);

0 commit comments

Comments
 (0)