ICU-13717 Fixes memory leak in date format parsing by moving cloning into the parseInt function.

X-SVN-Rev: 41437
This commit is contained in:
Shane Carr 2018-05-23 01:18:07 +00:00
parent 25d6d69068
commit 3e2bbc9834
2 changed files with 14 additions and 9 deletions

View File

@ -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<NumberFormat*>(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<DecimalFormat*>(fmt)) != NULL) {
const DecimalFormat* fmtAsDF = dynamic_cast<const DecimalFormat*>(fmt);
LocalPointer<DecimalFormat> df;
if (fmtAsDF != nullptr) {
df.adoptInstead(dynamic_cast<DecimalFormat*>(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);
}

View File

@ -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;