-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-117596: Fix realpath ValueError on null byte #117573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@barneygale or @encukou maybe on of you could have a quick look at this fix? Since you merged/reviewed a PR in this code area very recently. Thank you! |
2155d32
to
158db11
Compare
According to the devguide, I think this is not a trivial change like a typo fix, so we need to create an issue first if not exist and add |
b55c035
to
af81d82
Compare
Thanks! Created an issue and added entries to |
Calling os.path.realpath with a path containg a null byte ("\x00") it raised an ValueError on posix systems. This fix will catch the ValueError similar to how other path operations are handling such an Error. e.g. os.path.exists, os.path.ismount, os.fspath, os.path.islink
af81d82
to
25e5c1d
Compare
Misc/NEWS.d/next/Library/2024-04-07-06-46-43.gh-issue-117596.Mxo4gr.rst
Outdated
Show resolved
Hide resolved
25e5c1d
to
ed5bfb8
Compare
ed5bfb8
to
e2a4b68
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! Please could you add a test that ValueError
is still raised when strict=True
? Also mention that your change affects non-strict mode in the NEWS please.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
For future reference, please don't force-push to CPython PR branches -- it makes the changes a little harder to follow for reviewers, and every PR gets squashed anyway. |
Thank you, for your review! |
Thanks for making the requested changes! @barneygale: please review the changes made to this pull request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thank you. I'll wait a few days before merging just in case another reviewer spots an issue I've missed.
In 3.11+ on Windows,
except ValueError as ex:
# gh-106242: Raised for embedded null characters
# In strict mode, we convert into an OSError.
# Non-strict mode returns the path as-is, since we've already
# made it absolute.
if strict:
raise OSError(str(ex)) from None example: >>> os.path.realpath('\0')
'C:\\Temp\\\x00'
>>> os.path.realpath('\0', strict=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen ntpath>", line 704, in realpath
OSError: _getfinalpathname: embedded null character in path |
Good to know, I am not able to test with Windows. The missing tests you mentioned in your comment are also added with this PR. |
That's up to @barneygale and @zooba. |
IIRC, we changed to Strictly speaking, since it's never going to work without the caller changing the value (as opposed to the OS changing), |
Steve, in #106242 I argued against changing The behavior of
Is that a vote for changing it back to raising |
Ah, I was going for consistency with the earlier behaviour, which was the wrong behaviour as well by my definition above. (I do remember someone arguing that it wasn't documented though, so perhaps that was another issue? I don't think it was Eryk Sun.) I guess that's a vote for changing the Windows implementation to not replace that particular error, but there's no particular urgency (i.e. not in this PR). And I think non-strict mode should be as garbage-in-garbage-out as we can make it, so suppressing the error in that case is fine as long as the final string includes the text after the null. |
The following commit authors need to sign the Contributor License Agreement: |
Calling os.path.realpath with a path containg a null byte ("\x00") it raised an ValueError on posix systems.
This fix will catch the ValueError similar to how other path operations are handling such an Error. e.g.
os.path.exists
,os.path.ismount
,os.fspath, os.path.islink
The
ValueError
is initially raised byos.lstat
the other mentioned path operations are handing thisValueError
ofos.lstat
https://github.com/python/cpython/blob/main/Lib/genericpath.py#L30
https://github.com/python/cpython/blob/main/Lib/genericpath.py#L20
https://github.com/python/cpython/blob/main/Lib/genericpath.py#L64
https://github.com/python/cpython/blob/main/Lib/posixpath.py#L197
https://github.com/python/cpython/blob/main/Lib/posixpath.py#L213
os.path.realpath
raises aValueError
when path contains a null byte #117596