From 2755dcda5e32f26c97b3d178cf7d3ef6b9a33ca3 Mon Sep 17 00:00:00 2001 From: Michael Ow Date: Wed, 11 Feb 2015 22:23:13 +0000 Subject: [PATCH] ICU-11523 Fix invalid input check in udat_parseCalendar X-SVN-Rev: 37023 --- icu4c/source/i18n/udat.cpp | 22 +++++++++-------- icu4c/source/test/cintltst/cdattst.c | 37 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/icu4c/source/i18n/udat.cpp b/icu4c/source/i18n/udat.cpp index 35a2226136f..2ad49c88e21 100644 --- a/icu4c/source/i18n/udat.cpp +++ b/icu4c/source/i18n/udat.cpp @@ -1,6 +1,6 @@ /* ******************************************************************************* -* Copyright (C) 1996-2014, International Business Machines +* Copyright (C) 1996-2015, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************* */ @@ -313,19 +313,21 @@ udat_parseCalendar(const UDateFormat* format, const UnicodeString src((UBool)(textLength == -1), text, textLength); ParsePosition pp; + int32_t stackParsePos = 0; - if(parsePos != 0) - pp.setIndex(*parsePos); + if(parsePos == NULL) { + parsePos = &stackParsePos; + } + + pp.setIndex(*parsePos); ((DateFormat*)format)->parse(src, *(Calendar*)calendar, pp); - if(parsePos != 0) { - if(pp.getErrorIndex() == -1) - *parsePos = pp.getIndex(); - else { - *parsePos = pp.getErrorIndex(); - *status = U_PARSE_ERROR; - } + if(pp.getErrorIndex() == -1) + *parsePos = pp.getIndex(); + else { + *parsePos = pp.getErrorIndex(); + *status = U_PARSE_ERROR; } } diff --git a/icu4c/source/test/cintltst/cdattst.c b/icu4c/source/test/cintltst/cdattst.c index e1b53f117d3..fec9f2640ee 100644 --- a/icu4c/source/test/cintltst/cdattst.c +++ b/icu4c/source/test/cintltst/cdattst.c @@ -37,6 +37,7 @@ static void TestAllLocales(void); static void TestRelativeCrash(void); static void TestContext(void); static void TestCalendarDateParse(void); +static void TestParseErrorReturnValue(void); #define LEN(a) (sizeof(a)/sizeof(a[0])) @@ -56,6 +57,7 @@ void addDateForTest(TestNode** root) TESTCASE(TestContext); TESTCASE(TestCalendarDateParse); TESTCASE(TestOverrideNumberFormat); + TESTCASE(TestParseErrorReturnValue); } /* Testing the DateFormat API */ static void TestDateFormat() @@ -1697,4 +1699,39 @@ static void TestOverrideNumberFormat(void) { } } +/* + * Ticket #11523 + * udat_parse and udat_parseCalendar should have the same error code when given the same invalid input. + */ +static void TestParseErrorReturnValue(void) { + UErrorCode status = U_ZERO_ERROR; + UErrorCode expectStatus = U_PARSE_ERROR; + UDateFormat* df; + UCalendar* cal; + + df = udat_open(UDAT_DEFAULT, UDAT_DEFAULT, NULL, NULL, -1, NULL, -1, &status); + if (!assertSuccess("udat_open()", &status)) { + return; + } + + cal = ucal_open(NULL, 0, "en_US", UCAL_GREGORIAN, &status); + if (!assertSuccess("ucal_open()", &status)) { + return; + } + + udat_parse(df, NULL, -1, NULL, &status); + if (status != expectStatus) { + log_err("%s should have been returned by udat_parse when given an invalid input, instead got - %s\n", u_errorName(expectStatus), u_errorName(status)); + } + + status = U_ZERO_ERROR; + udat_parseCalendar(df, cal, NULL, -1, NULL, &status); + if (status != expectStatus) { + log_err("%s should have been returned by udat_parseCalendar when given an invalid input, instead got - %s\n", u_errorName(expectStatus), u_errorName(status)); + } + + ucal_close(cal); + udat_close(df); +} + #endif /* #if !UCONFIG_NO_FORMATTING */ -- 2.40.0