Skip to content
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

pathlib.Path.resolve() mishandles symlink loops #109187

Closed
barneygale opened this issue Sep 9, 2023 · 1 comment
Closed

pathlib.Path.resolve() mishandles symlink loops #109187

barneygale opened this issue Sep 9, 2023 · 1 comment
Labels
topic-pathlib type-bug An unexpected behavior, bug, or error

Comments

@barneygale
Copy link
Contributor

barneygale commented Sep 9, 2023

Bug report

Bug description:

Two closely-related issues in pathlib.Path.resolve():

First, resolve(strict=True) raises RuntimeError rather than OSError when a symlink loop is encountered. This is done only for backwards compatibility, since #25264 made pathlib call os.path.realpath(). It should raise OSError(ELOOP)

Second, resolve(strict=False) suppresses every kind of OS error except symlink loop errors. Again this is only for backwards compatibility. It should suppress exceptions about symlink loops.

Relevant code:

cpython/Lib/pathlib.py

Lines 1233 to 1252 in e21c89f

def check_eloop(e):
winerror = getattr(e, 'winerror', 0)
if e.errno == ELOOP or winerror == _WINERROR_CANT_RESOLVE_FILENAME:
raise RuntimeError("Symlink loop from %r" % e.filename)
try:
s = os.path.realpath(self, strict=strict)
except OSError as e:
check_eloop(e)
raise
p = self.with_segments(s)
# In non-strict mode, realpath() doesn't raise on symlink loops.
# Ensure we get an exception by calling stat()
if not strict:
try:
p.stat()
except OSError as e:
check_eloop(e)
return p

CPython versions tested on:

3.11, 3.12, CPython main branch

Operating systems tested on:

No response

Linked PRs

@barneygale barneygale added type-bug An unexpected behavior, bug, or error topic-pathlib labels Sep 9, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Sep 9, 2023
…ve()`

Treat symlink loops like other errors: in strict mode, raise `OSError`, and
in non-strict mode, do not raise any exception.
barneygale added a commit to barneygale/cpython that referenced this issue Sep 9, 2023
…ve()`

Treat symlink loops like other errors: in strict mode, raise `OSError`, and
in non-strict mode, do not raise any exception.
barneygale added a commit that referenced this issue Sep 26, 2023
…H-109192)

Treat symlink loops like other errors: in strict mode, raise `OSError`, and
in non-strict mode, do not raise any exception.
@barneygale
Copy link
Contributor Author

Addressed in 3.13 / ecd813f / #109192

csm10495 pushed a commit to csm10495/cpython that referenced this issue Sep 28, 2023
…ve()` (pythonGH-109192)

Treat symlink loops like other errors: in strict mode, raise `OSError`, and
in non-strict mode, do not raise any exception.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-pathlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant