From fd5a346b3de386b6e72ee72d17730f6cf116e9f5 Mon Sep 17 00:00:00 2001 From: Andy Heninger Date: Fri, 29 Oct 2021 17:17:41 -0700 Subject: [PATCH] ICU-21778 UnicodeString::clone error handling fix Change UnicodeString::clone() to return a nullptr if the underlying copy constructor produces a bogus string. This can happen if the copy constructor encounters a memory allocation failure in allocating the copy's internal string buffer, or if the string being copied was already bogus. The change is consistent with other ICU clone functions, which are generally defined to return nullptr in case of errors. --- icu4c/source/common/unistr.cpp | 3 ++- icu4c/source/test/intltest/ustrtest.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index 077b4d6ef20..918693898d1 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -334,7 +334,8 @@ Replaceable::clone() const { // UnicodeString overrides clone() with a real implementation UnicodeString * UnicodeString::clone() const { - return new UnicodeString(*this); + LocalPointer clonedString(new UnicodeString(*this)); + return clonedString.isValid() && !clonedString->isBogus() ? clonedString.orphan() : nullptr; } //======================================== diff --git a/icu4c/source/test/intltest/ustrtest.cpp b/icu4c/source/test/intltest/ustrtest.cpp index cd9751d761b..11bbb6e27af 100644 --- a/icu4c/source/test/intltest/ustrtest.cpp +++ b/icu4c/source/test/intltest/ustrtest.cpp @@ -1653,6 +1653,16 @@ UnicodeStringTest::TestBogus() { if(test1>=test2 || !(test2>test1) || test1.compare(test2)>=0 || !(test2.compare(test1)>0)) { errln("bogus