Skip to content

ICU-23444 Fix null pointer dereference and leak in SpoofData::reserveSpace on uprv_realloc failure#4051

Open
UnLucky252 wants to merge 1 commit into
unicode-org:mainfrom
UnLucky252:fix-ICU-23444-uspoof-impl
Open

ICU-23444 Fix null pointer dereference and leak in SpoofData::reserveSpace on uprv_realloc failure#4051
UnLucky252 wants to merge 1 commit into
unicode-org:mainfrom
UnLucky252:fix-ICU-23444-uspoof-impl

Conversation

@UnLucky252

Copy link
Copy Markdown

Linked Jira issue

ICU-23444

Summary

In SpoofData::reserveSpace (icu4c/source/i18n/uspoof_impl.cpp),
the result of uprv_realloc(fRawData, fMemLimit) was assigned
directly back to fRawData and immediately dereferenced
(fRawData->fLength = ...).

Cause

Two problems:

  1. Null pointer dereference on OOM. uprv_realloc returns
    nullptr on allocation failure; the next write through that
    nullptr is undefined behavior.
  2. Memory leak via the standard realloc gotcha. When
    realloc(p, n) fails it returns nullptr but does not free
    the original buffer. Assigning the result directly back to
    fRawData overwrites the live pointer with nullptr and loses
    the original allocation.

Fix

Take the uprv_realloc result into a temporary, check for nullptr,
set status = U_MEMORY_ALLOCATION_ERROR via the existing
UErrorCode& parameter and return without touching fRawData. On
success, 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

  • Required: Issue filed: ICU-23444
  • Required: The PR title must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
  • Required: Each commit message must be prefixed with a JIRA Issue number. Example: "ICU-NNNNN Fix xyz"
  • Issue accepted (done by Technical Committee after discussion)
  • Tests included, if applicable
  • API docs and/or User Guide docs changed or added, if applicable
  • Approver: Feel free to merge on my behalf

…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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant