From d049c395fc079cf61e1e3a853eda19b808e5e67a Mon Sep 17 00:00:00 2001 From: chaoticgd <43898262+chaoticgd@users.noreply.github.com> Date: Sun, 15 Mar 2026 23:39:55 +0000 Subject: [PATCH] SmallString: Improve self-assignment behaviour --- common/SmallString.cpp | 3 ++- tests/ctest/common/CMakeLists.txt | 1 + tests/ctest/common/small_string_tests.cpp | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/ctest/common/small_string_tests.cpp diff --git a/common/SmallString.cpp b/common/SmallString.cpp index 5f0efc3a30..7960cbeb8a 100644 --- a/common/SmallString.cpp +++ b/common/SmallString.cpp @@ -392,7 +392,8 @@ void SmallStringBase::vsprintf(const char* format, va_list ap) void SmallStringBase::assign(const SmallStringBase& copy) { - assign(copy.c_str(), copy.length()); + if (this != ©) + assign(copy.c_str(), copy.length()); } void SmallStringBase::assign(const char* str) diff --git a/tests/ctest/common/CMakeLists.txt b/tests/ctest/common/CMakeLists.txt index bb5ceb304b..b4aefd4625 100644 --- a/tests/ctest/common/CMakeLists.txt +++ b/tests/ctest/common/CMakeLists.txt @@ -2,6 +2,7 @@ add_pcsx2_test(common_test byteswap_tests.cpp filesystem_tests.cpp path_tests.cpp + small_string_tests.cpp string_util_tests.cpp ) diff --git a/tests/ctest/common/small_string_tests.cpp b/tests/ctest/common/small_string_tests.cpp new file mode 100644 index 0000000000..e2ae52c602 --- /dev/null +++ b/tests/ctest/common/small_string_tests.cpp @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2002-2026 PCSX2 Dev Team +// SPDX-License-Identifier: GPL-3.0+ + +#include "common/SmallString.h" +#include + +TEST(StackString, SelfAssignment) +{ + SmallStackString<6> string("Hello"); + string = string; + ASSERT_STREQ(string.c_str(), "Hello"); +}