]> granicus.if.org Git - icu/commitdiff
ICU-21559 more guards for self-assignment
authorMarkus Scherer <markus.icu@gmail.com>
Fri, 26 Mar 2021 21:09:11 +0000 (14:09 -0700)
committerMarkus Scherer <markus.icu@gmail.com>
Tue, 30 Mar 2021 19:19:11 +0000 (12:19 -0700)
16 files changed:
icu4c/source/common/edits.cpp
icu4c/source/i18n/cpdtrans.cpp
icu4c/source/i18n/dtfmtsym.cpp
icu4c/source/i18n/number_decimfmtprops.h
icu4c/source/i18n/number_fluent.cpp
icu4c/source/i18n/number_multiplier.cpp
icu4c/source/i18n/number_usageprefs.cpp
icu4c/source/i18n/numrange_fluent.cpp
icu4c/source/i18n/olsontz.cpp
icu4c/source/i18n/stsearch.cpp
icu4c/source/i18n/translit.cpp
icu4c/source/i18n/windtfmt.cpp
icu4c/source/i18n/winnmfmt.cpp
icu4c/source/test/intltest/numbertest_api.cpp
icu4c/source/test/intltest/tsdtfmsy.cpp
icu4c/source/test/intltest/tztest.cpp

index 95f0c19a728cf411cbfa97de5b8d4878186aafb2..92ca36fb5d04a3003d672b4cc929a3811a362816 100644 (file)
@@ -86,6 +86,7 @@ Edits &Edits::moveArray(Edits &src) U_NOEXCEPT {
 }
 
 Edits &Edits::operator=(const Edits &other) {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     length = other.length;
     delta = other.delta;
     numChanges = other.numChanges;
index 82ee54a77b560788b19e7ab4352f3fab8e5c0726..dc0217ba612ef8f1bee0c609b72331d97d223ed8 100644 (file)
@@ -282,6 +282,7 @@ void CompoundTransliterator::freeTransliterators(void) {
 CompoundTransliterator& CompoundTransliterator::operator=(
                                              const CompoundTransliterator& t)
 {
+    if (this == &t) { return *this; }  // self-assignment: no-op
     Transliterator::operator=(t);
     int32_t i = 0;
     UBool failed = FALSE;
index 81e3bf11090a908714dfe6df306cd2dce9e67838..937cf7036bc51f34e0f1da5af2c70319397c2c4b 100644 (file)
@@ -450,6 +450,7 @@ DateFormatSymbols::copyData(const DateFormatSymbols& other) {
  */
 DateFormatSymbols& DateFormatSymbols::operator=(const DateFormatSymbols& other)
 {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     dispose();
     copyData(other);
 
index 1ce84d9dc388d7164f0fc5d8df4f93e6bfb1ef91..0ace241adae8ab75dc58a2506328128a7df599f4 100644 (file)
@@ -38,7 +38,7 @@ namespace impl {
 
 // Exported as U_I18N_API because it is a public member field of exported DecimalFormatProperties
 // Using this wrapper is rather unfortunate, but is needed on Windows platforms in order to allow
-// for DLL-exporting an fully specified template instantiation.
+// for DLL-exporting a fully specified template instantiation.
 class U_I18N_API CurrencyPluralInfoWrapper {
 public:
     LocalPointer<CurrencyPluralInfo> fPtr;
@@ -52,7 +52,8 @@ public:
     }
 
     CurrencyPluralInfoWrapper& operator=(const CurrencyPluralInfoWrapper& other) {
-        if (!other.fPtr.isNull()) {
+        if (this != &other &&  // self-assignment: no-op
+                !other.fPtr.isNull()) {
             fPtr.adoptInstead(new CurrencyPluralInfo(*other.fPtr));
         }
         return *this;
index 0e27405132ccd7c50dfbffc3fccb933c01084d13..a79f224829d8053e743c2a4fddca1a444abd8358 100644 (file)
@@ -442,6 +442,7 @@ LocalizedNumberFormatter::LocalizedNumberFormatter(NFS<LNF>&& src) U_NOEXCEPT
 }
 
 LocalizedNumberFormatter& LocalizedNumberFormatter::operator=(const LNF& other) {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     NFS<LNF>::operator=(static_cast<const NFS<LNF>&>(other));
     UErrorCode localStatus = U_ZERO_ERROR; // Can't bubble up the error
     lnfCopyHelper(other, localStatus);
index 8f07e548de121b57b31a0d06badd2086e4736f8e..58e1e441bd28c55096c8459ceaa0af8b9dceb5cd 100644 (file)
@@ -46,6 +46,7 @@ Scale::Scale(const Scale& other)
 }
 
 Scale& Scale::operator=(const Scale& other) {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     fMagnitude = other.fMagnitude;
     if (other.fArbitrary != nullptr) {
         UErrorCode localStatus = U_ZERO_ERROR;
index ff285dbf972005a85f07432cf2d65f312660790a..ed426da086e32727709b5e7222e992edc395ba65 100644 (file)
@@ -34,6 +34,7 @@ StringProp::StringProp(const StringProp &other) : StringProp() {
 
 // Copy assignment operator
 StringProp &StringProp::operator=(const StringProp &other) {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     fLength = 0;
     fError = other.fError;
     if (fValue != nullptr) {
index d9286d1d713d2cecc5d803ee4c181a1360c53c99..f1060b3c21d45ea9239690daaf5a42ca5f11e86b 100644 (file)
@@ -245,6 +245,7 @@ LocalizedNumberRangeFormatter::LocalizedNumberRangeFormatter(NFS<LNF>&& src) U_N
 }
 
 LocalizedNumberRangeFormatter& LocalizedNumberRangeFormatter::operator=(const LNF& other) {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     NFS<LNF>::operator=(static_cast<const NFS<LNF>&>(other));
     // Do not steal; just clear
     delete fAtomicFormatter.exchange(nullptr);
index 1d0fa804b207cdb8f677056bf8f24ccdf0dadf96..b56c4a7bac20f80a77926ad4060030f39f620ad1 100644 (file)
@@ -274,6 +274,7 @@ OlsonTimeZone::OlsonTimeZone(const OlsonTimeZone& other) :
  * Assignment operator
  */
 OlsonTimeZone& OlsonTimeZone::operator=(const OlsonTimeZone& other) {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     canonicalID = other.canonicalID;
 
     transitionTimesPre32 = other.transitionTimesPre32;
index 3e6ed4648be94fdf854e9b30e95fae67687f5531..7fcd2bd93fef9d814c923838d083210ae015fec6 100644 (file)
@@ -184,7 +184,7 @@ StringSearch::clone() const {
 // operator overloading ---------------------------------------------
 StringSearch & StringSearch::operator=(const StringSearch &that)
 {
-    if ((*this) != that) {
+    if (this != &that) {
         UErrorCode status = U_ZERO_ERROR;
         m_text_          = that.m_text_;
         m_breakiterator_ = that.m_breakiterator_;
index ef44f42aa666c2ff8b0034043d676a73f5126288..9b2eaad77f06348a047d4c0a93552820457ee649 100644 (file)
@@ -170,6 +170,7 @@ Transliterator* Transliterator::clone() const {
  * Assignment operator.
  */
 Transliterator& Transliterator::operator=(const Transliterator& other) {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     ID = other.ID;
     // NUL-terminate the ID string
     ID.getTerminatedBuffer();
index bcf272bc612f1c7db33ddf545f72cce2fcc3b707..f6a990ea29e6098b3d91704bf183ac7ee80f37f0 100644 (file)
@@ -193,6 +193,7 @@ Win32DateFormat::~Win32DateFormat()
 
 Win32DateFormat &Win32DateFormat::operator=(const Win32DateFormat &other)
 {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     // The following handles fCalendar
     DateFormat::operator=(other);
 
index 72da1be28b8ecece692652cbade4d1f7942fd1af..8b2a9a4f958536727e5ee960ab47bda7dc022e5a 100644 (file)
@@ -268,6 +268,7 @@ Win32NumberFormat::~Win32NumberFormat()
 
 Win32NumberFormat &Win32NumberFormat::operator=(const Win32NumberFormat &other)
 {
+    if (this == &other) { return *this; }  // self-assignment: no-op
     NumberFormat::operator=(other);
 
     this->fCurrency          = other.fCurrency;
index 689af5c48852c5950729f15dfd874cab0260cc3b..eedf4524dfee3f05a9e5c8469aed509d2893d629 100644 (file)
@@ -1209,6 +1209,7 @@ void NumberFormatterApiTest::unitArbitraryMeasureUnits() {
               .unit(MeasureUnit::forIdentifier("pow4-mile", status))
               .unitWidth(UNUM_UNIT_WIDTH_FULL_NAME)
               .locale("en-ZA");
+    lnf.operator=(lnf);  // self-assignment should be a no-op
     lnf.formatInt(1, status);
     status.expectErrorAndReset(U_RESOURCE_TYPE_MISMATCH);
 
index c1efcc2ad85d88592c4a16d42e4f67f742c75a66..2c1a1832e7595b035ccc580355680b838e2a3a8c 100644 (file)
@@ -149,6 +149,7 @@ void IntlTestDateFormatSymbols::TestGetSetSpecificItems()
         dataerrln("ERROR: Couldn't create English DateFormatSymbols " + (UnicodeString)u_errorName(status));
         return;
     }
+    symbol->operator=(*symbol);  // self-assignment should be a no-op
     int32_t cntFmtAbbrev, cntFmtShort, cntStdAloneShort;
     const UnicodeString * wdFmtAbbrev     = symbol->getWeekdays(cntFmtAbbrev,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED);
     const UnicodeString * wdFmtShort      = symbol->getWeekdays(cntFmtShort,DateFormatSymbols::FORMAT,DateFormatSymbols::SHORT);
index 93d992f778f45d691c34607e7008df9633b66f00..b877db5161f80bb78dac103524edf4fa8f54fbba 100644 (file)
@@ -1171,8 +1171,10 @@ void TimeZoneTest::TestCustomParse()
         TimeZone *zone = TimeZone::createTimeZone(id);
         UnicodeString   itsID, temp;
 
-        if (dynamic_cast<OlsonTimeZone *>(zone) != NULL) {
+        OlsonTimeZone *ozone = dynamic_cast<OlsonTimeZone *>(zone);
+        if (ozone != nullptr) {
             logln(id + " -> Olson time zone");
+            ozone->operator=(*ozone);  // self-assignment should be a no-op
         } else {
             zone->getID(itsID);
             int32_t ioffset = zone->getRawOffset()/1000;