Skip to content

Commit

Permalink
ICU-22475 Fix double free in Locale under OOM
Browse files Browse the repository at this point in the history
See #2567
  • Loading branch information
FrankYFTang committed Aug 30, 2023
1 parent 02d5e71 commit 35bae68
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions icu4c/source/common/uloc_tag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2092,12 +2092,13 @@ ultag_parse(const char* tag, int32_t tagLen, int32_t* parsedLen, UErrorCode* sta
int32_t oldTagLength = tagLen;
if (tagLen < newTagLength) {
uprv_free(tagBuf);
tagBuf = (char*)uprv_malloc(newTagLength + 1);
// Change t->buf after the free and before return to avoid the second double free in
// the destructor of t when t is out of scope.
t->buf = tagBuf = (char*)uprv_malloc(newTagLength + 1);
if (tagBuf == nullptr) {
*status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
t->buf = tagBuf;
tagLen = newTagLength;
}
parsedLenDelta = checkLegacyLen - replacementLen;
Expand Down
4 changes: 4 additions & 0 deletions icu4c/source/test/cintltst/cloctst.c
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,10 @@ static void TestCanonicalization21749StackUseAfterScope(void)
input, u_errorName(status));
return;
}

// ICU-22475 test that we don't free an internal buffer twice.
status = U_ZERO_ERROR;
uloc_canonicalize("ti-defaultgR-lS-z-UK-0P", buffer, UPRV_LENGTHOF(buffer), &status);
}

static void TestDisplayKeywords(void)
Expand Down

0 comments on commit 35bae68

Please sign in to comment.