]> granicus.if.org Git - icu/commitdiff
ICU-20799 delete calendarToAdopt upon error
authorFrank Tang <ftang@chromium.org>
Thu, 29 Aug 2019 19:26:11 +0000 (12:26 -0700)
committerFrank Yung-Fong Tang <41213225+FrankYFTang@users.noreply.github.com>
Thu, 29 Aug 2019 22:49:37 +0000 (15:49 -0700)
ICU-20799 add unit tests

ICU-20799 initialize error and add comments.

icu4c/source/i18n/smpdtfmt.cpp
icu4c/source/test/intltest/dtfmttst.cpp
icu4c/source/test/intltest/dtfmttst.h

index 0b4939f8e85144fedea42c7f514ce893b05cde51..b8edff42098e5137341a1d4c98502e332ea8cc6b 100644 (file)
@@ -3999,6 +3999,7 @@ void SimpleDateFormat::adoptCalendar(Calendar* calendarToAdopt)
   DateFormatSymbols *newSymbols =
           DateFormatSymbols::createForLocale(calLocale, status);
   if (U_FAILURE(status)) {
+      delete calendarToAdopt;
       return;
   }
   DateFormat::adoptCalendar(calendarToAdopt);
index 197a3c3fd139fc7d6afeb236e75a70e09f575e48..128edba98fec35bda3a7c3585b16e24107e68627 100644 (file)
@@ -127,6 +127,7 @@ void DateFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &nam
     TESTCASE_AUTO(TestMinuteSecondFieldsInOddPlaces);
     TESTCASE_AUTO(TestDayPeriodParsing);
     TESTCASE_AUTO(TestParseRegression13744);
+    TESTCASE_AUTO(TestAdoptCalendarLeak);
 
     TESTCASE_AUTO_END;
 }
@@ -5540,6 +5541,19 @@ void DateFormatTest::TestParseRegression13744() {
     assertEquals("Error index", inDate.length(), pos.getErrorIndex());
 }
 
+void DateFormatTest::TestAdoptCalendarLeak() {
+    UErrorCode status = U_ZERO_ERROR;
+    // This test relies on the locale fullName exceeding ULOC_FULLNAME_CAPACITY
+    // in order for setKeywordValue to fail.
+    SimpleDateFormat sdf(
+        "d.M.y",
+        Locale("de__POSIX@colstrength=primary;currency=eur;em=default;"
+               "hours=h23;lb=strict;lw=normal;measure=metric;numbers=latn;"
+               "rg=atzzzz;sd=atat1;ss=none;timezone=Europe/Vienna"),
+        status);
+    sdf.adoptCalendar(Calendar::createInstance(status));
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */
 
 //eof
index efc56732494d35616477712aec3eec1da1ef9591..fd07b5ae0e3fc36c1bcca3b617132ba1e41a3a8b 100644 (file)
@@ -262,6 +262,7 @@ public:
     void TestMinuteSecondFieldsInOddPlaces();
     void TestDayPeriodParsing();
     void TestParseRegression13744();
+    void TestAdoptCalendarLeak();
 
 private:
     UBool showParse(DateFormat &format, const UnicodeString &formattedString);