From 35e950d35fea0cad481205981344c4983469e22f Mon Sep 17 00:00:00 2001 From: Hugo van der Merwe <17109322+hugovdm@users.noreply.github.com> Date: Wed, 18 Nov 2020 23:48:04 +0100 Subject: [PATCH] ICU-21387 measunit: check for nullptr before calling delete --- icu4c/source/i18n/measunit.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/icu4c/source/i18n/measunit.cpp b/icu4c/source/i18n/measunit.cpp index dab3abb5e21..c3e39b9c660 100644 --- a/icu4c/source/i18n/measunit.cpp +++ b/icu4c/source/i18n/measunit.cpp @@ -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 {