Skip to content

Commit cb59af3

Browse files
committed
Squashed 'src/minisketch/' changes from 3472e2f5ec..eb37a9b8e7
eb37a9b8e7 Merge bitcoin-core/minisketch#87: Avoid copy in self-assign fe6557642e Merge bitcoin-core/minisketch#88: build: Add `-Wundef` 8ea298bfa7 Avoid copy in self-assign 978a3d8869 build: Add `-Wundef` 3387044179 Merge bitcoin-core/minisketch#86: doc: fix typo in sketch_impl.h 15c2d13b60 doc: fix typo in sketch_impl.h 7be08b8a46 Merge bitcoin-core/minisketch#85: Fixes for integer precision loss 00fb4a4d83 Avoid or make integer precision conversion explicit 9d62a4d27c Avoid the need to cast/convert to size_t for vector operations 19e06cc7af Prevent overflows from large capacity/max_elements git-subtree-dir: src/minisketch git-subtree-split: eb37a9b8e79f9e49d73b96a49bf97a96d9eb676c
1 parent 1eea10a commit cb59af3

File tree

7 files changed

+15
-11
lines changed

7 files changed

+15
-11
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ case $host in
102102
esac
103103

104104
AX_CHECK_COMPILE_FLAG([-Wall],[WARN_CXXFLAGS="$WARN_CXXFLAGS -Wall"],,[[$CXXFLAG_WERROR]])
105+
AX_CHECK_COMPILE_FLAG([-Wundef], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wundef"], [], [$CXXFLAG_WERROR])
105106
AX_CHECK_COMPILE_FLAG([-fvisibility=hidden],[CXXFLAGS="$CXXFLAGS -fvisibility=hidden"],[],[$CXXFLAG_WERROR])
106107

107108
if test "x$use_ccache" != "xno"; then

include/minisketch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class Minisketch
239239
/** Make this Minisketch a clone of the specified one. */
240240
Minisketch& operator=(const Minisketch& sketch) noexcept
241241
{
242-
if (sketch.m_minisketch) {
242+
if (this != &sketch && sketch.m_minisketch) {
243243
m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_clone(sketch.m_minisketch.get()));
244244
}
245245
return *this;

src/false_positives.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ uint64_t BaseFPBits(uint32_t bits, uint32_t capacity) {
8181

8282
size_t ComputeCapacity(uint32_t bits, size_t max_elements, uint32_t fpbits) {
8383
if (bits == 0) return 0;
84-
uint64_t base_fpbits = BaseFPBits(bits, max_elements);
84+
if (max_elements > 0xffffffff) return max_elements;
85+
uint64_t base_fpbits = BaseFPBits(bits, static_cast<uint32_t>(max_elements));
8586
// The fpbits provided by the base max_elements==capacity case are sufficient.
8687
if (base_fpbits >= fpbits) return max_elements;
8788
// Otherwise, increment capacity by ceil(fpbits / bits) beyond that.
@@ -90,6 +91,7 @@ size_t ComputeCapacity(uint32_t bits, size_t max_elements, uint32_t fpbits) {
9091

9192
size_t ComputeMaxElements(uint32_t bits, size_t capacity, uint32_t fpbits) {
9293
if (bits == 0) return 0;
94+
if (capacity > 0xffffffff) return capacity;
9395
// Start with max_elements=capacity, and decrease max_elements until the corresponding capacity is capacity.
9496
size_t max_elements = capacity;
9597
while (true) {

src/int_utils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static inline int CountBits(I val, int max) {
159159
}
160160
if (!ret) return 0;
161161
return index + 1;
162-
#elif HAVE_CLZ
162+
#elif defined(HAVE_CLZ)
163163
(void)max;
164164
if (val == 0) return 0;
165165
if (std::numeric_limits<unsigned>::digits >= std::numeric_limits<I>::digits) {
@@ -210,7 +210,7 @@ class BitsInt {
210210
static constexpr inline int TopBits(I val) {
211211
static_assert(Count > 0, "BitsInt::TopBits needs Count > 0");
212212
static_assert(Count <= BITS, "BitsInt::TopBits needs Offset <= BITS");
213-
return val >> (BITS - Count);
213+
return static_cast<int>(val >> (BITS - Count));
214214
}
215215

216216
static inline constexpr I CondXorWith(I val, bool cond, I v) {

src/minisketch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ size_t minisketch_merge(minisketch* sketch, const minisketch* other_sketch) {
468468
ssize_t minisketch_decode(const minisketch* sketch, size_t max_elements, uint64_t* output) {
469469
const Sketch* s = (const Sketch*)sketch;
470470
s->Check();
471-
return s->Decode(max_elements, output);
471+
return s->Decode(static_cast<int>(max_elements), output);
472472
}
473473

474474
void minisketch_set_seed(minisketch* sketch, uint64_t seed) {

src/sketch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Sketch
2929
virtual ~Sketch() {}
3030
virtual size_t Syndromes() const = 0;
3131

32-
virtual void Init(int syndromes) = 0;
32+
virtual void Init(size_t syndromes) = 0;
3333
virtual void Add(uint64_t element) = 0;
3434
virtual void Serialize(unsigned char*) const = 0;
3535
virtual void Deserialize(const unsigned char*) = 0;

src/sketch_impl.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ template<typename F>
9292
void Sqr(std::vector<typename F::Elem>& poly, const F& field) {
9393
if (poly.size() == 0) return;
9494
poly.resize(poly.size() * 2 - 1);
95-
for (int x = poly.size() - 1; x >= 0; --x) {
95+
for (size_t i = 0; i < poly.size(); ++i) {
96+
auto x = poly.size() - i - 1;
9697
poly[x] = (x & 1) ? 0 : field.Sqr(poly[x / 2]);
9798
}
9899
}
@@ -217,7 +218,7 @@ bool RecFindRoots(std::vector<std::vector<typename F::Elem>>& stack, size_t pos,
217218
}
218219

219220
if (fully_factorizable) {
220-
// Every succesful iteration of this algorithm splits the input
221+
// Every successful iteration of this algorithm splits the input
221222
// polynomial further into buckets, each corresponding to a subset
222223
// of 2^(BITS-depth) roots. If after depth splits the degree of
223224
// the polynomial is >= 2^(BITS-depth), something is wrong.
@@ -297,7 +298,7 @@ std::vector<typename F::Elem> BerlekampMassey(const std::vector<typename F::Elem
297298
auto discrepancy = syndromes[n];
298299
for (size_t i = 1; i < current.size(); ++i) discrepancy ^= table[n - i](current[i]);
299300
if (discrepancy != 0) {
300-
int x = n + 1 - (current.size() - 1) - (prev.size() - 1);
301+
int x = static_cast<int>(n + 1 - (current.size() - 1) - (prev.size() - 1));
301302
if (!b_have_inv) {
302303
b_inv = field.Inv(b);
303304
b_have_inv = true;
@@ -366,7 +367,7 @@ class SketchImpl final : public Sketch
366367
}
367368

368369
size_t Syndromes() const override { return m_syndromes.size(); }
369-
void Init(int count) override { m_syndromes.assign(count, 0); }
370+
void Init(size_t count) override { m_syndromes.assign(count, 0); }
370371

371372
void Add(uint64_t val) override
372373
{
@@ -405,7 +406,7 @@ class SketchImpl final : public Sketch
405406
for (const auto& root : roots) {
406407
*(out++) = m_field.ToUint64(root);
407408
}
408-
return roots.size();
409+
return static_cast<int>(roots.size());
409410
}
410411

411412
size_t Merge(const Sketch* other_sketch) override

0 commit comments

Comments
 (0)