diff --git a/include/etl/basic_string.h b/include/etl/basic_string.h index 6d109277..914ca7eb 100644 --- a/include/etl/basic_string.h +++ b/include/etl/basic_string.h @@ -662,7 +662,10 @@ namespace etl //********************************************************************* void assign(const etl::ibasic_string& other) { - assign_impl(other.begin(), other.end(), other.is_truncated(), other.is_secure()); + if (&other != this) + { + assign_impl(other.begin(), other.end(), other.is_truncated(), other.is_secure()); + } } //********************************************************************* @@ -702,9 +705,9 @@ namespace etl /// Truncates if the string does not have enough free space. ///\param other The other string. //********************************************************************* - void assign(const_pointer other) + void assign(const_pointer text) { - assign(other, other + etl::strlen(other)); + assign_impl(text, text + etl::strlen(text), false, false); } //********************************************************************* @@ -713,9 +716,9 @@ namespace etl ///\param other The other string. ///\param length The length to copy. //********************************************************************* - void assign(const_pointer other, size_type length_) + void assign(const_pointer text, size_type length_) { - assign(other, other + length_); + assign_impl(text, text + length_, false, false); } //********************************************************************* @@ -724,7 +727,7 @@ namespace etl template void assign(const etl::basic_string_view& view) { - assign(view.begin(), view.end()); + assign_impl(view.begin(), view.end(), false, false); } //********************************************************************* @@ -2641,7 +2644,15 @@ namespace etl ETL_ASSERT(d >= 0, ETL_ERROR(string_iterator)); #endif - initialise(); +#if ETL_HAS_STRING_CLEAR_AFTER_USE + if (secure) + { + set_secure(); + } +#endif + + current_size = 0U; + cleanup(); while ((first != last) && (current_size != CAPACITY)) { @@ -2657,15 +2668,6 @@ namespace etl ETL_ASSERT(flags.test() == false, ETL_ERROR(string_truncation)); #endif #endif - -#if ETL_HAS_STRING_CLEAR_AFTER_USE - if (secure) - { - set_secure(); - } -#endif - - cleanup(); } //************************************************************************* diff --git a/test/test_string_char.cpp b/test/test_string_char.cpp index 7c118ff8..5521ddac 100644 --- a/test/test_string_char.cpp +++ b/test/test_string_char.cpp @@ -1082,6 +1082,20 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_self_assign_string) + { + Text text(initial_text.c_str()); + + text.assign(text); + + bool is_equal = Equal(initial_text, text); + CHECK(is_equal); + #if ETL_HAS_STRING_TRUNCATION_CHECKS + CHECK(!text.is_truncated()); + #endif + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assign_view) { @@ -1102,6 +1116,21 @@ namespace #endif } + //************************************************************************* + TEST_FIXTURE(SetupFixture, test_self_assign_view) + { + Text text(initial_text.c_str()); + View view(text); + + text.assign(view); + + bool is_equal = Equal(initial_text, text); + CHECK(is_equal); + #if ETL_HAS_STRING_TRUNCATION_CHECKS + CHECK(!text.is_truncated()); + #endif + } + //************************************************************************* TEST_FIXTURE(SetupFixture, test_assign_string_excess) {