Title: gh-151763: Fix NULL deref in `os._path_normpath()` by zainnadeem786 · Pull Request #151779 · python/cpython · GitHub
Open Graph Title: gh-151763: Fix NULL deref in `os._path_normpath()` by zainnadeem786 · Pull Request #151779 · python/cpython
X Title: gh-151763: Fix NULL deref in `os._path_normpath()` by zainnadeem786 · Pull Request #151779 · python/cpython
Description: 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 Unicode result using either PyUnicode_FromOrdinal() or PyUnicode_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 return NULL with a pending MemoryError. The existing code could then pass that NULL value into PyUnicode_EncodeFSDefault(), causing a crash. Fix Add an explicit NULL check immediately after creating result and before the bytes-path encoding branch. if (result == NULL) { return NULL; } This preserves the pending MemoryError and prevents PyUnicode_EncodeFSDefault() from receiving a NULL object. Validation I validated this using a CPython debug build on Windows: PCbuild\build.bat -p x64 -c Debug Then I used _testcapi.set_nomemory() to inject allocation failures while calling: os.path.normpath(b"a\\..\\b\\c") Before the fix With the NULL guard removed, allocation index 2 crashed: IDX 2 RC 3221225477 3221225477 is 0xC0000005, Windows STATUS_ACCESS_VIOLATION. After the fix With the NULL guard restored, the same allocation index returned a clean MemoryError instead of crashing: IDX 2 RC 10 MEMORYERROR This confirms that the failing OOM path now propagates the allocation failure instead of dereferencing NULL. Tests Focused ntpath tests passed: PCbuild\amd64\python_d.exe -m unittest -v ` test.test_ntpath.TestNtpath.test_normpath ` test.test_ntpath.TestNtpath.test_normpath_invalid_paths ` test.test_ntpath.NtCommonTest.test_normpath_issue5827 ` test.test_ntpath.NtCommonTest.test_normpath_issue106242 Result: Ran 4 tests in 0.009s OK I also checked the patch with: git diff --check -- Modules/posixmodule.c 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.
Open Graph Description: 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(...
X Description: 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(...
Opengraph URL: https://github.com/python/cpython/pull/151779
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:a4c8bb34-1964-396d-e541-3f6d897f340b |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | BC44:1461A1:184A0:1EAB9:6A52DDF1 |
| html-safe-nonce | 4c76708e273634f217b463dac37829c371864a9807d9fdab60326f06a00d9637 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCQzQ0OjE0NjFBMToxODRBMDoxRUFCOTo2QTUyRERGMSIsInZpc2l0b3JfaWQiOiIxNjQ5MjQ5NTExOTU5MzU0ODY1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d9ee8349b767b508c61f1ca08b0c5acdc32024003142f9010ccf04a4133d7f6b |
| hovercard-subject-tag | pull_request:3903133217 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/151779/files |
| twitter:image | https://avatars.githubusercontent.com/u/165761530?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/165761530?s=400&v=4 |
| og:image:alt | 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(... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width