Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
stefanhoelzl wants to merge5 commits intopython:main
base:main
Choose a base branch
Loading
fromstefanhoelzl:fix-realpath-value-error

Conversation

stefanhoelzl
Copy link
Contributor

@stefanhoelzlstefanhoelzl commentedApr 5, 2024
edited by bedevere-appbot
Loading

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

TheValueError 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

@stefanhoelzl
Copy link
ContributorAuthor

@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!

@stefanhoelzlstefanhoelzlforce-pushed thefix-realpath-value-error branch from2155d32 to158db11CompareApril 5, 2024 20:11
@barneygalebarneygale self-requested a reviewApril 5, 2024 20:36
@aisk
Copy link
Contributor

aisk commentedApr 6, 2024
edited
Loading

According to thedevguide, I think this is not a trivial change like a typo fix, so we need to create an issue first if not exist and addgh-{issue number}: at the beginning of the PR title. The news entry file is also required.

JelleZijlstra reacted with thumbs up emoji

@stefanhoelzlstefanhoelzl changed the titleFix realpath ValueError on null bytegh-117596: Fix realpath ValueError on null byteApr 7, 2024
@stefanhoelzlstefanhoelzlforce-pushed thefix-realpath-value-error branch fromb55c035 toaf81d82CompareApril 7, 2024 06:51
@stefanhoelzl
Copy link
ContributorAuthor

According to thedevguide, I think this is not a trivial change like a typo fix, so we need to create an issue first if not exist and addgh-{issue number}: at the beginning of the PR title. The news entry file is also required.

Thanks! Created an issue and added entries toMisc/NEWS.d/ andMisc/ACKS

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
@stefanhoelzlstefanhoelzlforce-pushed thefix-realpath-value-error branch fromaf81d82 to25e5c1dCompareApril 7, 2024 06:55
@stefanhoelzlstefanhoelzlforce-pushed thefix-realpath-value-error branch from25e5c1d toed5bfb8CompareApril 7, 2024 07:09
@stefanhoelzlstefanhoelzlforce-pushed thefix-realpath-value-error branch fromed5bfb8 toe2a4b68CompareApril 7, 2024 07:16
Copy link
Contributor

@barneygalebarneygale left a 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 thatValueError is still raised whenstrict=True? Also mention that your change affects non-strict mode in the NEWS please.

@bedevere-app
Copy link

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 phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@barneygale
Copy link
Contributor

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.

stefanhoelzl reacted with thumbs up emoji

@stefanhoelzl
Copy link
ContributorAuthor

Looking good! Please could you add a test thatValueError is still raised whenstrict=True? Also mention that your change affects non-strict mode in the NEWS please.

Thank you, for your review!
I have made the requested changes; please review again

@bedevere-app
Copy link

Thanks for making the requested changes!

@barneygale: please review the changes made to this pull request.

Copy link
Contributor

@barneygalebarneygale left a comment
edited
Loading

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.

stefanhoelzl reacted with thumbs up emoji
@eryksun
Copy link
Contributor

Looking good! Please could you add a test thatValueError is still raised whenstrict=True? Also mention that your change affects non-strict mode in the NEWS please.

In 3.11+ on Windows,realpath() raisesOSError for an embedded null in strict mode. This was changed by a PR for issue#106242. I was against changingnormpath() to raiseValueError for an embedded null, but therealpath() behavior was a separate case. I summarized therealpath() case inthis comment. On Windows, it had been mistakenly raisingValueError for an embedded null since 3.8. When thestrict parameter was added in 3.10, it should have been fixed to ignore theValueError in the default non-strict mode. Steve fixed it in 3.11, but for some reason he also decided to convert theValueError toOSError in strict mode.

ntpath source:

exceptValueErrorasex:# 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.ifstrict:raiseOSError(str(ex))fromNone

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 realpathOSError: _getfinalpathname: embedded null character in path

@stefanhoelzl
Copy link
ContributorAuthor

Steve fixed it in 3.11, but for some reason he also decided to convert theValueError toOSError in strict mode.

Good to know, I am not able to test with Windows.
Should I convert theValueError toOSError in strict mode to be consistent with the Windows implementation?

The missing tests you mentioned inyour comment are also added with this PR.

@eryksun
Copy link
Contributor

Should I convert theValueError toOSError in strict mode to be consistent with the Windows implementation?

That's up to@barneygale and@zooba.

@zooba
Copy link
Member

IIRC, we changed toOSError because someone pointed out that we didn't specify thatValueError might be raised (and apparently I either didn't win the argument that "ValueError is always implied when you pass a bad value", or I couldn't be bothered arguing that day).

Strictly speaking, since it's never going to work without the caller changing the value (as opposed to the OS changing),ValueError is correct.

@eryksun
Copy link
Contributor

Steve, in#106242 I argued against changingnormpath() to raiseValueError. You opted to modify the C implementation of_path_normpath() instead, so the discussion aboutValueError became irrelevant.

The behavior ofrealpath() also came up. On Windows, it started raisingValueError for embedded null characters in 3.8, and similarly on POSIX in 3.10. Personally, I just wantedrealpath() to ignoreValueError in the default permissive mode.

Strictly speaking, since it's never going to work without the caller changing the value (as opposed to the OS changing),ValueError is correct.

Is that a vote for changing it back to raisingValueError in strict mode on Windows? Or has that ship sailed?

@zooba
Copy link
Member

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.

@python-cla-bot
Copy link

The following commit authors need to sign the Contributor License Agreement:

CLA signed

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@aiskaiskaisk left review comments

@barneygalebarneygalebarneygale approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@stefanhoelzl@aisk@barneygale@eryksun@zooba

[8]ページ先頭

©2009-2025 Movatter.jp