From 17738c5eb39a2d0677265ef9a96cbe0953f7abaa Mon Sep 17 00:00:00 2001 From: Derek Ludwig Date: Fri, 8 Mar 2024 13:37:48 -0800 Subject: [PATCH] fix(unique_ptr): assigning nullptr to a null unique_ptr causes an assert --- include/etl/memory.h | 10 ++++++++-- test/test_memory.cpp | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/etl/memory.h b/include/etl/memory.h index c4eb8fa98..1abcf6380 100644 --- a/include/etl/memory.h +++ b/include/etl/memory.h @@ -1424,7 +1424,10 @@ namespace etl //********************************* unique_ptr& operator =(std::nullptr_t) ETL_NOEXCEPT { - reset(nullptr); + if (p) + { + reset(nullptr); + } return *this; } @@ -1432,7 +1435,10 @@ namespace etl //********************************* unique_ptr& operator =(void*) ETL_NOEXCEPT { - reset(NULL); + if (p) + { + reset(NULL); + } return *this; } diff --git a/test/test_memory.cpp b/test/test_memory.cpp index 69beb3178..0e93e8c34 100644 --- a/test/test_memory.cpp +++ b/test/test_memory.cpp @@ -924,6 +924,17 @@ namespace CHECK(!bool(up)); } + //************************************************************************* + TEST(test_unique_ptr_nullptr_from_nullptr_assignment) + { + etl::unique_ptr up; + + up = nullptr; + + CHECK(up.get() == nullptr); + CHECK(!bool(up)); + } + //************************************************************************* TEST(test_unique_ptr_move_assignment) {