From: Shane Carr Date: Wed, 23 May 2018 01:18:07 +0000 (+0000) Subject: ICU-13717 Fixes memory leak in date format parsing by moving cloning into the parseIn... X-Git-Tag: release-62-rc~58^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3e2bbc98347bacc8197bc04d5b30851f89d49c79;p=icu ICU-13717 Fixes memory leak in date format parsing by moving cloning into the parseInt function. X-SVN-Rev: 41437 --- diff --git a/icu4c/source/i18n/smpdtfmt.cpp b/icu4c/source/i18n/smpdtfmt.cpp index a29ffc6aca5..fe993f6e105 100644 --- a/icu4c/source/i18n/smpdtfmt.cpp +++ b/icu4c/source/i18n/smpdtfmt.cpp @@ -2809,7 +2809,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC UErrorCode status = U_ZERO_ERROR; ParsePosition pos(0); UDateFormatField patternCharIndex = DateFormatSymbols::getPatternCharIndex(ch); - NumberFormat *currentNumberFormat; + const NumberFormat *currentNumberFormat; UnicodeString temp; UBool gotNumber = FALSE; @@ -2821,7 +2821,7 @@ int32_t SimpleDateFormat::subParse(const UnicodeString& text, int32_t& start, UC return -start; } - currentNumberFormat = dynamic_cast(getNumberFormatByIndex(patternCharIndex)->clone()); + currentNumberFormat = getNumberFormatByIndex(patternCharIndex); if (currentNumberFormat == NULL) { return -start; } @@ -3649,7 +3649,7 @@ void SimpleDateFormat::parseInt(const UnicodeString& text, Formattable& number, ParsePosition& pos, UBool allowNegative, - NumberFormat *fmt) const { + const NumberFormat *fmt) const { parseInt(text, number, -1, pos, allowNegative,fmt); } @@ -3661,16 +3661,21 @@ void SimpleDateFormat::parseInt(const UnicodeString& text, int32_t maxDigits, ParsePosition& pos, UBool allowNegative, - NumberFormat *fmt) const { + const NumberFormat *fmt) const { UnicodeString oldPrefix; - DecimalFormat* df = NULL; - if (!allowNegative && (df = dynamic_cast(fmt)) != NULL) { + const DecimalFormat* fmtAsDF = dynamic_cast(fmt); + LocalPointer df; + if (fmtAsDF != nullptr) { + df.adoptInstead(dynamic_cast(fmtAsDF->clone())); + } + if (!allowNegative && !df.isNull()) { + fmt = df.getAlias(); df->getNegativePrefix(oldPrefix); df->setNegativePrefix(UnicodeString(TRUE, SUPPRESS_NEGATIVE_PREFIX, -1)); } int32_t oldPos = pos.getIndex(); fmt->parse(text, number, pos); - if (df != NULL) { + if (!df.isNull()) { df->setNegativePrefix(oldPrefix); } diff --git a/icu4c/source/i18n/unicode/smpdtfmt.h b/icu4c/source/i18n/unicode/smpdtfmt.h index 498bc007d7e..a860021ab8e 100644 --- a/icu4c/source/i18n/unicode/smpdtfmt.h +++ b/icu4c/source/i18n/unicode/smpdtfmt.h @@ -1420,14 +1420,14 @@ private: Formattable& number, ParsePosition& pos, UBool allowNegative, - NumberFormat *fmt) const; + const NumberFormat *fmt) const; void parseInt(const UnicodeString& text, Formattable& number, int32_t maxDigits, ParsePosition& pos, UBool allowNegative, - NumberFormat *fmt) const; + const NumberFormat *fmt) const; int32_t checkIntSuffix(const UnicodeString& text, int32_t start, int32_t patLoc, UBool isNegative) const;