ICU-23444 Fix null pointer dereference and leak in SpoofData::reserveSpace on uprv_realloc failure#4051
Open
UnLucky252 wants to merge 1 commit into
Open
Conversation
…Space on uprv_realloc failure uprv_realloc result was assigned directly to fRawData and then dereferenced (fRawData->fLength = ...). On OOM uprv_realloc returns nullptr and the dereference is undefined behavior. Additionally, overwriting fRawData with the realloc result loses the original pointer if realloc fails — the standard realloc memory leak. Take the result into a temporary, check for nullptr, set U_MEMORY_ALLOCATION_ERROR via the existing UErrorCode& parameter and return without touching fRawData. On success, assign the new pointer back to fRawData. Found by static analysis (Svace, ISP RAS).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linked Jira issue
ICU-23444
Summary
In
SpoofData::reserveSpace(icu4c/source/i18n/uspoof_impl.cpp),the result of
uprv_realloc(fRawData, fMemLimit)was assigneddirectly back to
fRawDataand immediately dereferenced(
fRawData->fLength = ...).Cause
Two problems:
uprv_reallocreturnsnullptr on allocation failure; the next write through that
nullptr is undefined behavior.
reallocgotcha. Whenrealloc(p, n)fails it returns nullptr but does not freethe original buffer. Assigning the result directly back to
fRawDataoverwrites the live pointer with nullptr and losesthe original allocation.
Fix
Take the
uprv_reallocresult into a temporary, check for nullptr,set
status = U_MEMORY_ALLOCATION_ERRORvia the existingUErrorCode¶meter and return without touchingfRawData. Onsuccess, assign the new pointer back to
fRawData.Testing
Existing tests pass (no behavior change on the success path). No new
test added — the OOM path requires allocator injection that is not
part of the standard test harness.
Notes
Found by static analysis (Svace, ISP RAS).
Checklist