gh-151763: Fix NULL deref in os._path_normpath()#151779
Open
zainnadeem786 wants to merge 3 commits into
Open
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
os._path_normpath()
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.
Summary
This PR addresses OOM-0028 from gh-151763.
It fixes a NULL dereference in
os__path_normpath_impl()when memory allocation fails while normalizing a bytes path.Issue
os__path_normpath_impl()builds a Unicoderesultusing eitherPyUnicode_FromOrdinal()orPyUnicode_FromWideChar().For bytes input, the function then converts that Unicode result back to bytes using
PyUnicode_EncodeFSDefault().Before this change, the result of
PyUnicode_FromWideChar()was not checked before the bytes-path encoding branch.Under memory pressure,
PyUnicode_FromWideChar()can returnNULLwith a pendingMemoryError. The existing code could then pass thatNULLvalue intoPyUnicode_EncodeFSDefault(), causing a crash.Fix
Add an explicit NULL check immediately after creating
resultand before the bytes-path encoding branch.This preserves the pending
MemoryErrorand preventsPyUnicode_EncodeFSDefault()from receiving a NULL object.Validation
I validated this using a CPython debug build on Windows:
Then I used
_testcapi.set_nomemory()to inject allocation failures while calling:Before the fix
With the NULL guard removed, allocation index
2crashed:3221225477is0xC0000005, WindowsSTATUS_ACCESS_VIOLATION.After the fix
With the NULL guard restored, the same allocation index returned a clean
MemoryErrorinstead of crashing:This confirms that the failing OOM path now propagates the allocation failure instead of dereferencing NULL.
Tests
Focused
ntpathtests passed:Result:
I also checked the patch with:
No regression test is included in this PR. The OOM reproducer depends on allocation-failure injection and allocation indexes that can be build-sensitive. The fix itself is a minimal local NULL guard.
Addresses OOM-0028 from gh-151763.