Skip to content

Commit abc7bef

Browse files
committed
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.
1 parent ea8f66b commit abc7bef

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/bench.cpp

Lines changed: 8 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,13 @@ 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+
uint64_t upper_bound;
47+
if (bits < 64) {
48+
upper_bound = (uint64_t(1) << bits) - 1;
49+
} else {
50+
upper_bound = std::numeric_limits<uint64_t>::max();
51+
}
52+
std::uniform_int_distribution<uint64_t> dist(1, upper_bound);
4653
states.resize(iters);
4754
std::vector<double> benches;
4855
benches.reserve(iters);

0 commit comments

Comments
 (0)