From e8f3d5c65700e53a235507f9e0882a6ca5fe2a71 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Fri, 21 Aug 2020 00:16:23 +0200 Subject: [PATCH] ICU-21035 Replace backward compatibility ulocimp_getCountry() overload. By always calling the dynamic memory allocation implementation directly instead, the fixed memory buffer boundary gets pushed one step further towards the edges. --- icu4c/source/common/loclikely.cpp | 3 +-- icu4c/source/common/uloc.cpp | 26 +++++++++++--------------- icu4c/source/common/ulocimp.h | 6 +++--- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/icu4c/source/common/loclikely.cpp b/icu4c/source/common/loclikely.cpp index 41a12352bf..94a60aba3e 100644 --- a/icu4c/source/common/loclikely.cpp +++ b/icu4c/source/common/loclikely.cpp @@ -509,8 +509,7 @@ parseTagString( } } - subtagLength = ulocimp_getCountry(position, region, *regionLength, &position); - u_terminateChars(region, *regionLength, subtagLength, err); + subtagLength = ulocimp_getCountry(position, &position, *err).extract(region, *regionLength, *err); if(U_FAILURE(*err)) { goto error; diff --git a/icu4c/source/common/uloc.cpp b/icu4c/source/common/uloc.cpp index 5acf312ed3..95e17d3bce 100644 --- a/icu4c/source/common/uloc.cpp +++ b/icu4c/source/common/uloc.cpp @@ -1215,7 +1215,7 @@ ulocimp_getScript(const char *localeID, return result; } -static CharString +CharString U_EXPORT2 ulocimp_getCountry(const char *localeID, const char **pEnd, UErrorCode &status) { @@ -1250,14 +1250,6 @@ ulocimp_getCountry(const char *localeID, return result; } -U_CFUNC int32_t -ulocimp_getCountry(const char *localeID, - char *country, int32_t countryCapacity, - const char **pEnd) { - ErrorCode status; - return ulocimp_getCountry(localeID, pEnd, status).extract(country, countryCapacity, status); -} - /** * @param needSeparator if true, then add leading '_' if any variants * are added to 'variant' @@ -1458,7 +1450,10 @@ uloc_openKeywords(const char* localeID, } /* Skip the Country */ if (_isIDSeparator(*tmpLocaleID)) { - ulocimp_getCountry(tmpLocaleID+1, NULL, 0, &tmpLocaleID); + ulocimp_getCountry(tmpLocaleID+1, &tmpLocaleID, *status); + if (U_FAILURE(*status)) { + return 0; + } if(_isIDSeparator(*tmpLocaleID)) { _getVariant(tmpLocaleID+1, *tmpLocaleID, NULL, 0); } @@ -1750,8 +1745,6 @@ uloc_getCountry(const char* localeID, int32_t countryCapacity, UErrorCode* err) { - int32_t i=0; - if(err==NULL || U_FAILURE(*err)) { return 0; } @@ -1778,10 +1771,10 @@ uloc_getCountry(const char* localeID, localeID = scriptID; } if(_isIDSeparator(*localeID)) { - i=ulocimp_getCountry(localeID+1, country, countryCapacity, NULL); + return ulocimp_getCountry(localeID+1, NULL, *err).extract(country, countryCapacity, *err); } } - return u_terminateChars(country, countryCapacity, i, err); + return u_terminateChars(country, countryCapacity, 0, err); } U_CAPI int32_t U_EXPORT2 @@ -1827,7 +1820,10 @@ uloc_getVariant(const char* localeID, /* Skip the Country */ if (_isIDSeparator(*tmpLocaleID)) { const char *cntryID; - ulocimp_getCountry(tmpLocaleID+1, NULL, 0, &cntryID); + ulocimp_getCountry(tmpLocaleID+1, &cntryID, *err); + if (U_FAILURE(*err)) { + return 0; + } if (cntryID != tmpLocaleID+1) { /* Found optional country */ tmpLocaleID = cntryID; diff --git a/icu4c/source/common/ulocimp.h b/icu4c/source/common/ulocimp.h index 279f177f39..8879b2bc2a 100644 --- a/icu4c/source/common/ulocimp.h +++ b/icu4c/source/common/ulocimp.h @@ -59,10 +59,10 @@ ulocimp_getScript(const char *localeID, const char **pEnd, UErrorCode &status); -U_CFUNC int32_t +icu::CharString U_EXPORT2 ulocimp_getCountry(const char *localeID, - char *country, int32_t countryCapacity, - const char **pEnd); + const char **pEnd, + UErrorCode &status); U_STABLE void U_EXPORT2 ulocimp_getName(const char* localeID,