]> granicus.if.org Git - icu/commitdiff
ICU-21387 measunit: check for nullptr before calling delete
authorHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Wed, 18 Nov 2020 22:48:04 +0000 (23:48 +0100)
committerHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Tue, 24 Nov 2020 23:19:07 +0000 (00:19 +0100)
icu4c/source/i18n/measunit.cpp

index dab3abb5e21ff61cef41d03ccd181752a592e61f..c3e39b9c660d89ea4ac61ff280e46d8d18240405 100644 (file)
@@ -2105,7 +2105,9 @@ MeasureUnit &MeasureUnit::operator=(const MeasureUnit &other) {
     if (this == &other) {
         return *this;
     }
-    delete fImpl;
+    if (fImpl != nullptr) {
+        delete fImpl;
+    }
     if (other.fImpl) {
         ErrorCode localStatus;
         fImpl = new MeasureUnitImpl(other.fImpl->copy(localStatus));
@@ -2126,7 +2128,9 @@ MeasureUnit &MeasureUnit::operator=(MeasureUnit &&other) noexcept {
     if (this == &other) {
         return *this;
     }
-    delete fImpl;
+    if (fImpl != nullptr) {
+        delete fImpl;
+    }
     fImpl = other.fImpl;
     other.fImpl = nullptr;
     fTypeId = other.fTypeId;
@@ -2139,8 +2143,10 @@ MeasureUnit *MeasureUnit::clone() const {
 }
 
 MeasureUnit::~MeasureUnit() {
-    delete fImpl;
-    fImpl = nullptr;
+    if (fImpl != nullptr) {
+        delete fImpl;
+        fImpl = nullptr;
+    }
 }
 
 const char *MeasureUnit::getType() const {
@@ -2298,8 +2304,10 @@ void MeasureUnit::initCurrency(StringPiece isoCurrency) {
 void MeasureUnit::setTo(int32_t typeId, int32_t subTypeId) {
     fTypeId = typeId;
     fSubTypeId = subTypeId;
-    delete fImpl;
-    fImpl = nullptr;
+    if (fImpl != nullptr) {
+        delete fImpl;
+        fImpl = nullptr;
+    }
 }
 
 int32_t MeasureUnit::getOffset() const {