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.
// UnicodeString overrides clone() with a real implementation
UnicodeString *
UnicodeString::clone() const {
- return new UnicodeString(*this);
+ LocalPointer<UnicodeString> clonedString(new UnicodeString(*this));
+ return clonedString.isValid() && !clonedString->isBogus() ? clonedString.orphan() : nullptr;
}
//========================================
if(test1>=test2 || !(test2>test1) || test1.compare(test2)>=0 || !(test2.compare(test1)>0)) {
errln("bogus<empty failed");
}
+
+ // test that copy constructor of bogus is bogus & clone of bogus is nullptr
+ {
+ test3.setToBogus();
+ UnicodeString test3Copy(test3);
+ UnicodeString *test3Clone = test3.clone();
+ assertTrue(WHERE, test3.isBogus());
+ assertTrue(WHERE, test3Copy.isBogus());
+ assertTrue(WHERE, test3Clone == nullptr);
+ }
}
// StringEnumeration ------------------------------------------------------- ***