Skip to content

os.path.realpath() RecursionError #128098

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

Open
jwilk opened this issue Dec 19, 2024 · 5 comments
Open

os.path.realpath() RecursionError #128098

jwilk opened this issue Dec 19, 2024 · 5 comments
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@jwilk
Copy link

jwilk commented Dec 19, 2024

os.path.realpath() attempts to detect symlink loops, but this is not bulletproof;
it's possible to make it fall into infinite recursion.

Reproducer:

import os
import tempfile

with tempfile.TemporaryDirectory() as tmpdir:
    os.chdir(tmpdir)
    os.symlink('../' * (1 + tmpdir.count('/')) + f'{tmpdir}/d', 'd')
    os.path.realpath('d')

Traceback:

Traceback (most recent call last):
  File ".../recursiverealpath.py", line 7, in <module>
    os.path.realpath('d')
  File "<frozen posixpath>", line 427, in realpath
  File "<frozen posixpath>", line 487, in _joinrealpath
  File "<frozen posixpath>", line 487, in _joinrealpath
  File "<frozen posixpath>", line 487, in _joinrealpath
  [Previous line repeated 993 more times]
  File "<frozen posixpath>", line 442, in _joinrealpath
  File "<frozen posixpath>", line 63, in isabs
RecursionError: maximum recursion depth exceeded

Tested with Python 3.12.8.

@picnixz picnixz added extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes labels Dec 20, 2024
@keainye
Copy link

keainye commented Jan 7, 2025

This issue no longer reproduces in the the latest version (Python 3.14.0a3+ (heads/main:f802c8bf87, Jan 7 2025, 08:12:30) [GCC 12.2.0] on linux).

And in my opinion it seems to be an issue with os.symlink as it doesn't handle illegal paths...

@jwilk
Copy link
Author

jwilk commented Jan 7, 2025

This issue no longer reproduces in the the latest version

Looks like this was indeed fixed in abfa16b ("GH-114847: Speed up posixpath.realpath() (#114848)").

in my opinion it seems to be an issue with os.symlink as it doesn't handle illegal paths...

No, os.symlink() works as expected.

@picnixz
Copy link
Member

picnixz commented Feb 8, 2025

@barneygale Should we backport gh-114848 to fix the issue or is there something else we should do for 3.12 and 3.13?

@barneygale
Copy link
Contributor

I think #114848 landed in 3.13 already? Not sure about backporting to 3.12 - it's close to EOL and the fix is invasive

@picnixz
Copy link
Member

picnixz commented Feb 8, 2025

Ok, yes indeed it landed in 3.13.0a6. I think we can close this one as wont fix for 3.12 and completed for 3.13+.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants