Skip to content

Commit fa7f04c

Browse files
author
MarcoFalke
committed
refactor: Remove UB in prevector reverse iterators
rend() creates a pointer with offset -1. This is UB, according to the C++ standard: https://eel.is/c++draft/expr.add#4: When an expression J that has integral type is added to [...] an expression P of pointer type, the result has the type of P. ... if P points to a (possibly-hypothetical) array element i of an array object x with n elements [...] the expressions P + J and J + P (where J has the value j) point to the (possibly-hypothetical) array element i+j of x if 0≤i+j≤n [...] Otherwise, the behavior is undefined. Also, it is unclear why the functions exist at all, when stdlib utils such as std::reverse_iterator{it} or std::views::reverse can be used out of the box. So remove them, along with the ubsan suppressions, that are no longer used.
1 parent f9d8910 commit fa7f04c

File tree

2 files changed

+1
-49
lines changed

2 files changed

+1
-49
lines changed

src/prevector.h

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2015-2022 The Bitcoin Core developers
1+
// Copyright (c) 2015-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -77,26 +77,6 @@ class prevector {
7777
bool operator<(iterator x) const { return ptr < x.ptr; }
7878
};
7979

80-
class reverse_iterator {
81-
T* ptr{};
82-
public:
83-
typedef Diff difference_type;
84-
typedef T value_type;
85-
typedef T* pointer;
86-
typedef T& reference;
87-
typedef std::bidirectional_iterator_tag iterator_category;
88-
reverse_iterator() = default;
89-
reverse_iterator(T* ptr_) : ptr(ptr_) {}
90-
T& operator*() const { return *ptr; }
91-
T* operator->() const { return ptr; }
92-
reverse_iterator& operator--() { ptr++; return *this; }
93-
reverse_iterator& operator++() { ptr--; return *this; }
94-
reverse_iterator operator++(int) { reverse_iterator copy(*this); ++(*this); return copy; }
95-
reverse_iterator operator--(int) { reverse_iterator copy(*this); --(*this); return copy; }
96-
bool operator==(reverse_iterator x) const { return ptr == x.ptr; }
97-
bool operator!=(reverse_iterator x) const { return ptr != x.ptr; }
98-
};
99-
10080
class const_iterator {
10181
const T* ptr{};
10282
public:
@@ -129,27 +109,6 @@ class prevector {
129109
bool operator<(const_iterator x) const { return ptr < x.ptr; }
130110
};
131111

132-
class const_reverse_iterator {
133-
const T* ptr{};
134-
public:
135-
typedef Diff difference_type;
136-
typedef const T value_type;
137-
typedef const T* pointer;
138-
typedef const T& reference;
139-
typedef std::bidirectional_iterator_tag iterator_category;
140-
const_reverse_iterator() = default;
141-
const_reverse_iterator(const T* ptr_) : ptr(ptr_) {}
142-
const_reverse_iterator(reverse_iterator x) : ptr(&(*x)) {}
143-
const T& operator*() const { return *ptr; }
144-
const T* operator->() const { return ptr; }
145-
const_reverse_iterator& operator--() { ptr++; return *this; }
146-
const_reverse_iterator& operator++() { ptr--; return *this; }
147-
const_reverse_iterator operator++(int) { const_reverse_iterator copy(*this); ++(*this); return copy; }
148-
const_reverse_iterator operator--(int) { const_reverse_iterator copy(*this); --(*this); return copy; }
149-
bool operator==(const_reverse_iterator x) const { return ptr == x.ptr; }
150-
bool operator!=(const_reverse_iterator x) const { return ptr != x.ptr; }
151-
};
152-
153112
private:
154113
#pragma pack(push, 1)
155114
union direct_or_indirect {
@@ -304,11 +263,6 @@ class prevector {
304263
iterator end() { return iterator(item_ptr(size())); }
305264
const_iterator end() const { return const_iterator(item_ptr(size())); }
306265

307-
reverse_iterator rbegin() { return reverse_iterator(item_ptr(size() - 1)); }
308-
const_reverse_iterator rbegin() const { return const_reverse_iterator(item_ptr(size() - 1)); }
309-
reverse_iterator rend() { return reverse_iterator(item_ptr(-1)); }
310-
const_reverse_iterator rend() const { return const_reverse_iterator(item_ptr(-1)); }
311-
312266
size_t capacity() const {
313267
if (is_direct()) {
314268
return N;

test/sanitizer_suppressions/ubsan

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,13 @@ unsigned-integer-overflow:DecompressAmount
5454
unsigned-integer-overflow:crypto/
5555
unsigned-integer-overflow:MurmurHash3
5656
unsigned-integer-overflow:TxConfirmStats::EstimateMedianVal
57-
unsigned-integer-overflow:prevector.h
5857
unsigned-integer-overflow:InsecureRandomContext::rand64
5958
unsigned-integer-overflow:InsecureRandomContext::SplitMix64
6059
unsigned-integer-overflow:bitset_detail::PopCount
6160
implicit-integer-sign-change:SetStdinEcho
6261
implicit-integer-sign-change:compressor.h
6362
implicit-integer-sign-change:crypto/
6463
implicit-integer-sign-change:TxConfirmStats::removeTx
65-
implicit-integer-sign-change:prevector.h
6664
implicit-integer-sign-change:verify_flags
6765
implicit-integer-sign-change:EvalScript
6866
implicit-integer-sign-change:serialize.h

0 commit comments

Comments
 (0)