Skip to content

Commit

Permalink
C++: handle self-assignment correctly (OpenCyphal#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-kirienko authored Mar 8, 2023
1 parent a2b24f0 commit 602bfc6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/nunavut/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
.. autodata:: __version__
"""

__version__ = "2.0.7"
__version__ = "2.0.8"
__license__ = "MIT"
__author__ = "OpenCyphal"
__copyright__ = "Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. Copyright (c) 2023 OpenCyphal."
Expand Down
8 changes: 8 additions & 0 deletions src/nunavut/lang/cpp/support/variable_length_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ class VariableLengthArray
std::declval<allocator_type&>())) && noexcept(VariableLengthArray<T, MaxSize, Allocator>().reserve(1)) &&
std::is_nothrow_copy_constructible<allocator_type>::value && std::is_nothrow_copy_constructible<T>::value)
{
if (this == &rhs)
{
return *this;
}
fast_deallocate(data_, size_, capacity_, alloc_);
data_ = nullptr;
capacity_ = 0;
Expand Down Expand Up @@ -1136,6 +1140,10 @@ class VariableLengthArray<bool, MaxSize, Allocator>
std::declval<allocator_type&>())) && noexcept(VariableLengthArray<value_type, MaxSize, Allocator>()
.reserve(1)))
{
if (this == &rhs)
{
return *this;
}
fast_deallocate(data_, size_, capacity_, alloc_);
data_ = nullptr;
capacity_ = 0;
Expand Down
21 changes: 21 additions & 0 deletions verification/cpp/suite/test_var_len_arr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,27 @@ TYPED_TEST(VLATestsNonTrivialCommon, TestForEachConstIterators)
ASSERT_EQ(MaxSize, i);
}

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
#endif

TYPED_TEST(VLATestsNonTrivialCommon, SelfAssignment)
{
nunavut::support::VariableLengthArray<TypeParam, 20> subject;
subject.push_back(0);
subject.push_back(1);
ASSERT_EQ(2U, subject.size());
subject = subject;
ASSERT_EQ(2U, subject.size());
ASSERT_EQ(0, subject[0]);
ASSERT_EQ(1, subject[1]);
}

#if defined(__clang__)
# pragma clang diagnostic pop
#endif

// +----------------------------------------------------------------------+
/**
* Test suite to ensure non-trivial objects are properly handled. This one contains non-generic cases.
Expand Down

0 comments on commit 602bfc6

Please sign in to comment.