From 06ec8f531ec2d9a5ea54c6b7dfa07a99e5a7c5d5 Mon Sep 17 00:00:00 2001 From: Shane Carr Date: Fri, 15 Feb 2019 00:45:31 -0800 Subject: [PATCH] ICU-20254 Handling OOM in AffixTokenMatcherWarehouse. --- icu4c/source/i18n/numparse_affixes.cpp | 18 +++++++++++++++--- icu4c/source/i18n/numparse_affixes.h | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/icu4c/source/i18n/numparse_affixes.cpp b/icu4c/source/i18n/numparse_affixes.cpp index cc7fc8a6972..6e00d9f6be1 100644 --- a/icu4c/source/i18n/numparse_affixes.cpp +++ b/icu4c/source/i18n/numparse_affixes.cpp @@ -109,7 +109,12 @@ void AffixPatternMatcherBuilder::consumeToken(AffixPatternType type, UChar32 cp, } else { // Case 3: the token is a non-ignorable literal. - addMatcher(fWarehouse.nextCodePointMatcher(cp)); + if (auto* ptr = fWarehouse.nextCodePointMatcher(cp, status)) { + addMatcher(*ptr); + } else { + // OOM; unwind the stack + return; + } } fLastTypeOrCp = type != TYPE_CODEPOINT ? type : cp; } @@ -152,8 +157,15 @@ IgnorablesMatcher& AffixTokenMatcherWarehouse::ignorables() { return fSetupData->ignorables; } -NumberParseMatcher& AffixTokenMatcherWarehouse::nextCodePointMatcher(UChar32 cp) { - return *fCodePoints.create(cp); +NumberParseMatcher* AffixTokenMatcherWarehouse::nextCodePointMatcher(UChar32 cp, UErrorCode& status) { + if (U_FAILURE(status)) { + return nullptr; + } + auto* result = fCodePoints.create(cp); + if (result == nullptr) { + status = U_MEMORY_ALLOCATION_ERROR; + } + return result; } diff --git a/icu4c/source/i18n/numparse_affixes.h b/icu4c/source/i18n/numparse_affixes.h index 8b693624fa5..e02b17ba2d0 100644 --- a/icu4c/source/i18n/numparse_affixes.h +++ b/icu4c/source/i18n/numparse_affixes.h @@ -99,7 +99,7 @@ class U_I18N_API AffixTokenMatcherWarehouse : public UMemory { IgnorablesMatcher& ignorables(); - NumberParseMatcher& nextCodePointMatcher(UChar32 cp); + NumberParseMatcher* nextCodePointMatcher(UChar32 cp, UErrorCode& status); private: // NOTE: The following field may be unsafe to access after construction is done! -- 2.40.0