ICU-23418 Fix null pointer dereferences in SearchIterator constructors on allocation failure#4011
Open
UnLucky252 wants to merge 1 commit into
Open
ICU-23418 Fix null pointer dereferences in SearchIterator constructors on allocation failure#4011UnLucky252 wants to merge 1 commit into
UnLucky252 wants to merge 1 commit into
Conversation
…s on allocation failure All four SearchIterator constructors used the result of uprv_malloc(sizeof(USearch)) to initialize the USearch fields without a NULL check, which is undefined behavior on OOM. These are protected base-class constructors with no UErrorCode; subclasses such as StringSearch already propagate U_MEMORY_ALLOCATION_ERROR. The destructor already guards for nullptr. Guard the field initialization in each constructor with if (m_search_ != nullptr). In the CharacterIterator constructor, text.getText(m_text_) is moved before the guard so it always runs. 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-23418
Summary
All four
SearchIteratorconstructors (icu4c/source/i18n/search.cpp)used the result of
uprv_malloc(sizeof(USearch))to initialize theUSearchfields without a NULL check.Cause
On OOM
uprv_mallocreturns NULL; every field write is then undefinedbehavior.
Fix
These are protected base-class constructors with no
UErrorCode(subclasses such as
StringSearchalready propagateU_MEMORY_ALLOCATION_ERROR, and the destructor already guards fornullptr). Guard the field initialization in each constructor with
if (m_search_ != nullptr). In theCharacterIteratorconstructor,text.getText(m_text_)is moved before the guard so it always runs.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