diff options
author | Antonio Sanchez <cantonios@google.com> | 2021-03-05 12:54:26 -0800 |
---|---|---|
committer | Rasmus Munk Larsen <rmlarsen@google.com> | 2021-03-10 16:55:20 +0000 |
commit | 543e34ab9dee6004337b7ad912417eb91bf4a0b9 (patch) | |
tree | f250fa260325479d4006f0dec77f5ce2bdc009c5 /Eigen | |
parent | b8d1857f0d87475016b5c16a6a235efa1fd5e9b5 (diff) |
Re-implement move assignments.
The original swap approach leads to potential undefined behavior (reading
uninitialized memory) and results in unnecessary copying of data for static
storage.
Here we pass down the move assignment to the underlying storage. Static
storage does a one-way copy, dynamic storage does a swap.
Modified the tests to no longer read from the moved-from matrix/tensor,
since that can lead to UB. Added a test to ensure we do not access
uninitialized memory in a move.
Fixes: #2119
Diffstat (limited to 'Eigen')
-rw-r--r-- | Eigen/src/Core/Array.h | 2 | ||||
-rw-r--r-- | Eigen/src/Core/Matrix.h | 2 | ||||
-rw-r--r-- | Eigen/src/Core/PlainObjectBase.h | 4 |
3 files changed, 4 insertions, 4 deletions
diff --git a/Eigen/src/Core/Array.h b/Eigen/src/Core/Array.h index 64fd02ddf..9a61665a9 100644 --- a/Eigen/src/Core/Array.h +++ b/Eigen/src/Core/Array.h @@ -157,7 +157,7 @@ class Array EIGEN_DEVICE_FUNC Array& operator=(Array&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value) { - other.swap(*this); + Base::operator=(std::move(other)); return *this; } #endif diff --git a/Eigen/src/Core/Matrix.h b/Eigen/src/Core/Matrix.h index fb7238265..053003caf 100644 --- a/Eigen/src/Core/Matrix.h +++ b/Eigen/src/Core/Matrix.h @@ -278,7 +278,7 @@ class Matrix EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Matrix& operator=(Matrix&& other) EIGEN_NOEXCEPT_IF(std::is_nothrow_move_assignable<Scalar>::value) { - other.swap(*this); + Base::operator=(std::move(other)); return *this; } #endif diff --git a/Eigen/src/Core/PlainObjectBase.h b/Eigen/src/Core/PlainObjectBase.h index 595a6c13f..ca5b5ee1d 100644 --- a/Eigen/src/Core/PlainObjectBase.h +++ b/Eigen/src/Core/PlainObjectBase.h @@ -508,8 +508,8 @@ class PlainObjectBase : public internal::dense_xpr_base<Derived>::type EIGEN_DEVICE_FUNC PlainObjectBase& operator=(PlainObjectBase&& other) EIGEN_NOEXCEPT { - using std::swap; - swap(m_storage, other.m_storage); + _check_template_params(); + m_storage = std::move(other.m_storage); return *this; } #endif |