From 3e676f91167b9ad9e98f0a19392415f8f0e6a4ce Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Fri, 2 Aug 2024 02:35:38 -0700 Subject: [PATCH] ICU-22825 Fix memLeak during error in tznames_impl.cpp Rewrite the TextTrieMap::put() which should delete the value during error instead of deleting key. Rewrite to simplified the error handling. --- icu4c/source/i18n/tznames_impl.cpp | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/icu4c/source/i18n/tznames_impl.cpp b/icu4c/source/i18n/tznames_impl.cpp index 8c338b9cc7f1..9b7ade7f0bb1 100644 --- a/icu4c/source/i18n/tznames_impl.cpp +++ b/icu4c/source/i18n/tznames_impl.cpp @@ -235,24 +235,18 @@ TextTrieMap::put(const char16_t *key, void *value, UErrorCode &status) { LocalPointer lpLazyContents(new UVector(status), status); fLazyContents = lpLazyContents.orphan(); } - if (U_FAILURE(status)) { - if (fValueDeleter) { - fValueDeleter((void*) key); + if (U_SUCCESS(status)) { + U_ASSERT(fLazyContents != nullptr); + char16_t *s = const_cast(key); + fLazyContents->addElement(s, status); + if (U_SUCCESS(status)) { + fLazyContents->addElement(value, status); + return; } - return; } - U_ASSERT(fLazyContents != nullptr); - - char16_t *s = const_cast(key); - fLazyContents->addElement(s, status); - if (U_FAILURE(status)) { - if (fValueDeleter) { - fValueDeleter((void*) key); - } - return; + if (fValueDeleter) { + fValueDeleter(value); } - - fLazyContents->addElement(value, status); } void