Skip to content

Commit

Permalink
[RandomAccessIterator] Fix access to non-existent member
Browse files Browse the repository at this point in the history
Exposed by teslamotors#128

This was previously building without issue due to two-phase lookup and
the function never being called. New units tests will instantiate
this function.

```C++
template <class T>
struct MockTemplate
{
    int foo(const MockTemplate<T>& input) const { return input.variable_that_does_not_exist; }
};

int main()
{
    MockTemplate<int> it{};
    (void)it;
    // it.foo(it);
}

```
  • Loading branch information
alexkaratarakis committed Jun 20, 2024
1 parent 6e36ef9 commit 0cc2216
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/fixed_containers/random_access_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class RandomAccessIterator

friend constexpr Self operator+(difference_type n, const Self& other)
{
return Self(std::next(other.iterator_, n));
return Self{std::next(other, n)};
}

constexpr Self& operator-=(difference_type n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class RandomAccessIteratorTransformer

friend constexpr Self operator+(difference_type off, const Self& other)
{
return Self(std::next(other.iterator_, off), other.unary_function_);
return Self{std::next(other.iterator_, off), other.unary_function_};
}

constexpr Self& operator-=(difference_type off)
Expand Down
10 changes: 10 additions & 0 deletions test/fixed_circular_deque_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cstddef>
#include <deque>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <type_traits>

Expand All @@ -30,6 +31,9 @@ static_assert(TriviallyCopyable<CircularDequeType>);
static_assert(NotTrivial<CircularDequeType>);
static_assert(StandardLayout<CircularDequeType>);
static_assert(IsStructuralType<CircularDequeType>);

static_assert(std::random_access_iterator<CircularDequeType::iterator>);
static_assert(std::random_access_iterator<CircularDequeType::const_iterator>);
} // namespace trivially_copyable_vector

struct ComplexStruct
Expand Down Expand Up @@ -912,6 +916,9 @@ TEST(FixedCircularDeque, TrivialIterators)
static_assert(*std::prev(v1.end(), 1) == 99);
static_assert(*std::prev(v1.end(), 2) == 88);
static_assert(*std::prev(v1.end(), 3) == 77);

static_assert(*(1 + v1.begin()) == 88);
static_assert(*(2 + v1.begin()) == 99);
}

{
Expand Down Expand Up @@ -1032,6 +1039,9 @@ TEST(FixedCircularDeque, ReverseIterators)
static_assert(*std::prev(v1.rend(), 1) == 77);
static_assert(*std::prev(v1.rend(), 2) == 88);
static_assert(*std::prev(v1.rend(), 3) == 99);

static_assert(*(1 + v1.begin()) == 88);
static_assert(*(2 + v1.begin()) == 99);
}

{
Expand Down
11 changes: 11 additions & 0 deletions test/fixed_deque_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <cstddef>
#include <deque>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <type_traits>
#include <utility>
Expand All @@ -31,6 +32,10 @@ static_assert(TriviallyCopyable<DequeType>);
static_assert(NotTrivial<DequeType>);
static_assert(StandardLayout<DequeType>);
static_assert(IsStructuralType<DequeType>);

static_assert(std::random_access_iterator<DequeType::iterator>);
static_assert(std::random_access_iterator<DequeType::const_iterator>);

} // namespace trivially_copyable_deque

struct ComplexStruct
Expand Down Expand Up @@ -864,6 +869,9 @@ TEST(FixedDeque, TrivialIterators)
static_assert(*std::prev(v1.end(), 1) == 99);
static_assert(*std::prev(v1.end(), 2) == 88);
static_assert(*std::prev(v1.end(), 3) == 77);

static_assert(*(1 + v1.begin()) == 88);
static_assert(*(2 + v1.begin()) == 99);
}

{
Expand Down Expand Up @@ -984,6 +992,9 @@ TEST(FixedDeque, ReverseIterators)
static_assert(*std::prev(v1.rend(), 1) == 77);
static_assert(*std::prev(v1.rend(), 2) == 88);
static_assert(*std::prev(v1.rend(), 3) == 99);

static_assert(*(1 + v1.rbegin()) == 88);
static_assert(*(2 + v1.rbegin()) == 77);
}

{
Expand Down
9 changes: 9 additions & 0 deletions test/fixed_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ static_assert(NotTrivial<FixedStringType>);
static_assert(StandardLayout<FixedStringType>);
static_assert(IsStructuralType<FixedStringType>);

static_assert(std::contiguous_iterator<FixedStringType::iterator>);
static_assert(std::contiguous_iterator<FixedStringType::const_iterator>);

void const_span_ref(const std::span<char>&) {}
void const_span_of_const_ref(const std::span<const char>&) {}

Expand Down Expand Up @@ -483,6 +486,9 @@ TEST(FixedString, TrivialIterators)
static_assert(*std::prev(v1.end(), 1) == '9');
static_assert(*std::prev(v1.end(), 2) == '8');
static_assert(*std::prev(v1.end(), 3) == '7');

static_assert(*(1 + v1.begin()) == '8');
static_assert(*(2 + v1.begin()) == '9');
}

{
Expand Down Expand Up @@ -551,6 +557,9 @@ TEST(FixedString, ReverseIterators)
static_assert(*std::prev(v1.rend(), 1) == '7');
static_assert(*std::prev(v1.rend(), 2) == '8');
static_assert(*std::prev(v1.rend(), 3) == '9');

static_assert(*(1 + v1.begin()) == '8');
static_assert(*(2 + v1.begin()) == '9');
}

{
Expand Down
6 changes: 6 additions & 0 deletions test/fixed_vector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,9 @@ TEST(FixedVector, TrivialIterators)
static_assert(*std::prev(v1.end(), 1) == 99);
static_assert(*std::prev(v1.end(), 2) == 88);
static_assert(*std::prev(v1.end(), 3) == 77);

static_assert(*(1 + v1.begin()) == 88);
static_assert(*(2 + v1.begin()) == 99);
}

{
Expand Down Expand Up @@ -1007,6 +1010,9 @@ TEST(FixedVector, ReverseIterators)
static_assert(*std::prev(v1.rend(), 1) == 77);
static_assert(*std::prev(v1.rend(), 2) == 88);
static_assert(*std::prev(v1.rend(), 3) == 99);

static_assert(*(1 + v1.begin()) == 88);
static_assert(*(2 + v1.begin()) == 99);
}

{
Expand Down

0 comments on commit 0cc2216

Please sign in to comment.