From: Brett Cannon Date: Sat, 10 Jul 2004 22:58:32 +0000 (+0000) Subject: posixpath.realpath() now detects loops from symlinks and returns the longest X-Git-Tag: v2.3.5c1~184 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=36fd97cf0f3ced2fb206a1f7b0766c3569f2915f;p=thirdparty%2FPython%2Fcpython.git posixpath.realpath() now detects loops from symlinks and returns the longest path before recursion. --- diff --git a/Lib/posixpath.py b/Lib/posixpath.py index 1d225ae7d3cd..5ee25843f714 100644 --- a/Lib/posixpath.py +++ b/Lib/posixpath.py @@ -408,13 +408,37 @@ symbolic links encountered in the path.""" bits = ['/'] + filename.split('/')[1:] for i in range(2, len(bits)+1): component = join(*bits[0:i]) - if islink(component): - resolved = os.readlink(component) - (dir, file) = split(component) - resolved = normpath(join(dir, resolved)) - newpath = join(*([resolved] + bits[i:])) - return realpath(newpath) + # Resolve symbolic links. + if islink(component): + resolved = _resolve_link(component) + if resolved is None: + # Infinite loop -- return original component + rest of the path + return join(*([component] + bits[i:])) + else: + newpath = join(*([resolved] + bits[i:])) + return realpath(newpath) return filename + +def _resolve_link(path): + """Internal helper function. Takes a path and follows symlinks + until we either arrive at something that isn't a symlink, or + encounter a path we've seen before (meaning that there's a loop). + """ + paths_seen = [] + while islink(path): + if path in paths_seen: + # Already seen this path, so we must have a symlink loop + return None + paths_seen.append(path) + # Resolve where the link points to + resolved = os.readlink(path) + if not abspath(resolved): + dir = dirname(path) + path = normpath(join(dir, resolved)) + else: + path = normpath(resolved) + return path + supports_unicode_filenames = False diff --git a/Misc/NEWS b/Misc/NEWS index 46050d7cb800..4ac67d4be5e5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -40,6 +40,9 @@ Extension modules Library ------- +- Bug #930024: posixpath.realpath() now detects loops from symlinks and returns + the longest path before the loop begins. + - Bug #980327/Patch #988607: ntpath now compresses extra slashes between the drive letter and the rest of the path properly. Also removed ambiguity from UNC paths. Thanks Paul Moore.