From 7db28d3803bf8ff39391f65cd38de2bfda5bb9ae Mon Sep 17 00:00:00 2001 From: Murai Takashi Date: Wed, 30 Jan 2019 19:53:17 +0900 Subject: [PATCH] kits/locale: Fix PVS 495 Fix memory leak when realloc() fails. Change-Id: I9062bb177919805e9973c5afd6bc01f8fbb753b6 Reviewed-on: https://review.haiku-os.org/c/1016 Reviewed-by: Barrett17 --- src/kits/locale/Collator.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/kits/locale/Collator.cpp b/src/kits/locale/Collator.cpp index ca9e8eb260..401557ab98 100644 --- a/src/kits/locale/Collator.cpp +++ b/src/kits/locale/Collator.cpp @@ -142,9 +142,14 @@ BCollator::GetSortKey(const char* string, BString* key) const int requiredSize = fICUCollator->getSortKey(UnicodeString(string, length, NULL, error), buffer, length * 2); if (requiredSize > length * 2) { - buffer = (uint8_t*)realloc(buffer, requiredSize); - if (buffer == NULL) + uint8_t* tmpBuffer = (uint8_t*)realloc(buffer, requiredSize); + if (tmpBuffer == NULL) { + free(buffer); + buffer = NULL; return B_NO_MEMORY; + } else { + buffer = tmpBuffer; + } error = U_ZERO_ERROR; fICUCollator->getSortKey(UnicodeString(string, length, NULL, error),