From 973a96785e85104508d46dc4539083f6a5021229 Mon Sep 17 00:00:00 2001 From: Jeff Genovy <29107334+jefgen@users.noreply.github.com> Date: Fri, 30 Aug 2019 12:03:16 -0700 Subject: [PATCH] ICU-20802 OOM not handled in uloc_openKeywordList. Also change to use LocalPointer and nullptr instead of NULL. --- icu4c/source/common/uloc.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/icu4c/source/common/uloc.cpp b/icu4c/source/common/uloc.cpp index 5a12697b85e..d58bdfa64e9 100644 --- a/icu4c/source/common/uloc.cpp +++ b/icu4c/source/common/uloc.cpp @@ -1457,31 +1457,32 @@ static const UEnumeration gKeywordsEnum = { U_CAPI UEnumeration* U_EXPORT2 uloc_openKeywordList(const char *keywordList, int32_t keywordListSize, UErrorCode* status) { - UKeywordsContext *myContext = NULL; - UEnumeration *result = NULL; + LocalPointer myContext; + LocalPointer result; - if(U_FAILURE(*status)) { - return NULL; + if (U_FAILURE(*status)) { + return nullptr; } - result = (UEnumeration *)uprv_malloc(sizeof(UEnumeration)); - /* Null pointer test */ - if (result == NULL) { - *status = U_MEMORY_ALLOCATION_ERROR; - return NULL; + result.adoptInsteadAndCheckErrorCode(static_cast(uprv_malloc(sizeof(UEnumeration))), *status); + if (U_FAILURE(*status)) { + return nullptr; + } + uprv_memcpy(result.getAlias(), &gKeywordsEnum, sizeof(UEnumeration)); + + myContext.adoptInsteadAndCheckErrorCode(static_cast(uprv_malloc(sizeof(UKeywordsContext))), *status); + if (U_FAILURE(*status)) { + return nullptr; } - uprv_memcpy(result, &gKeywordsEnum, sizeof(UEnumeration)); - myContext = static_cast(uprv_malloc(sizeof(UKeywordsContext))); - if (myContext == NULL) { + myContext->keywords = static_cast(uprv_malloc(keywordListSize+1)); + if (myContext->keywords == nullptr) { *status = U_MEMORY_ALLOCATION_ERROR; - uprv_free(result); - return NULL; + return nullptr; } - myContext->keywords = (char *)uprv_malloc(keywordListSize+1); uprv_memcpy(myContext->keywords, keywordList, keywordListSize); myContext->keywords[keywordListSize] = 0; myContext->current = myContext->keywords; - result->context = myContext; - return result; + result->context = myContext.orphan(); + return result.orphan(); } U_CAPI UEnumeration* U_EXPORT2 -- 2.40.0