Skip to content

Use os.lstat in findall to correctly detect symlinked directories#260

Open
bysiber wants to merge 1 commit into
pypa:masterfrom
bysiber:fix/findall-lstat-symlinks
Open

Use os.lstat in findall to correctly detect symlinked directories#260
bysiber wants to merge 1 commit into
pypa:masterfrom
bysiber:fix/findall-lstat-symlinks

Conversation

@bysiber
Copy link
Copy Markdown

@bysiber bysiber commented Feb 20, 2026

Summary

Manifest.findall() uses os.stat() which follows symlinks, making the S_ISLNK(mode) check dead code — symlinked directories are always traversed.

Problem

stat = os.stat(fullname)
mode = stat.st_mode
if S_ISREG(mode):
    allfiles.append(fsdecode(fullname))
elif S_ISDIR(mode) and not S_ISLNK(mode):
    push(fullname)

os.stat() follows symbolic links, so stat.st_mode always reflects the target of the symlink, never the symlink itself. S_ISLNK(mode) can only return True when the mode comes from os.lstat(), which returns info about the link itself.

As a result, symlinked directories are always added to the traversal stack. With circular symlinks (e.g. a directory that symlinks to a parent), this leads to infinite recursion.

Fix

Use os.lstat() instead of os.stat() so that S_ISLNK(mode) works as intended.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant