Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd4b545

Browse files
authored
GH-118289: Fix handling of non-directories in posixpath.realpath() (#120127)
In strict mode, raise `NotADirectoryError` if we encounter a non-directory while we still have path parts left to process. We use a `part_count` variable rather than `len(rest)` because the `rest` stack also contains markers for unresolved symlinks.
1 parent c695e37 commit fd4b545

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

Lib/posixpath.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ def _realpath(filename, strict=False, sep=sep, curdir=curdir, pardir=pardir,
412412
# very fast way of spelling list(reversed(...)).
413413
rest = filename.split(sep)[::-1]
414414

415+
# Number of unprocessed parts in 'rest'. This can differ from len(rest)
416+
# later, because 'rest' might contain markers for unresolved symlinks.
417+
part_count = len(rest)
418+
415419
# The resolved path, which is absolute throughout this function.
416420
# Note: getcwd() returns a normalized and symlink-free path.
417421
path = sep if filename.startswith(sep) else getcwd()
@@ -426,12 +430,13 @@ def _realpath(filename, strict=False, sep=sep, curdir=curdir, pardir=pardir,
426430
# by *maxlinks*, this is used instead of *seen* to detect symlink loops.
427431
link_count = 0
428432

429-
while rest:
433+
while part_count:
430434
name = rest.pop()
431435
if name is None:
432436
# resolved symlink target
433437
seen[rest.pop()] = path
434438
continue
439+
part_count -= 1
435440
if not name or name == curdir:
436441
# current dir
437442
continue
@@ -444,8 +449,11 @@ def _realpath(filename, strict=False, sep=sep, curdir=curdir, pardir=pardir,
444449
else:
445450
newpath = path + sep + name
446451
try:
447-
st = lstat(newpath)
448-
if not stat.S_ISLNK(st.st_mode):
452+
st_mode = lstat(newpath).st_mode
453+
if not stat.S_ISLNK(st_mode):
454+
if strict and part_count and not stat.S_ISDIR(st_mode):
455+
raise OSError(errno.ENOTDIR, os.strerror(errno.ENOTDIR),
456+
newpath)
449457
path = newpath
450458
continue
451459
elif maxlinks is not None:
@@ -487,7 +495,9 @@ def _realpath(filename, strict=False, sep=sep, curdir=curdir, pardir=pardir,
487495
rest.append(newpath)
488496
rest.append(None)
489497
# Push the unresolved symlink target parts onto the stack.
490-
rest.extend(target.split(sep)[::-1])
498+
target_parts = target.split(sep)[::-1]
499+
rest.extend(target_parts)
500+
part_count += len(target_parts)
491501

492502
return path
493503

Lib/test/test_posixpath.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,65 @@ def test_realpath_unreadable_symlink(self):
695695
os.chmod(ABSTFN, 0o755, follow_symlinks=False)
696696
os.unlink(ABSTFN)
697697

698+
@skip_if_ABSTFN_contains_backslash
699+
def test_realpath_nonterminal_file(self):
700+
try:
701+
with open(ABSTFN, 'w') as f:
702+
f.write('test_posixpath wuz ere')
703+
self.assertEqual(realpath(ABSTFN, strict=False), ABSTFN)
704+
self.assertEqual(realpath(ABSTFN, strict=True), ABSTFN)
705+
self.assertEqual(realpath(ABSTFN + "/", strict=False), ABSTFN)
706+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=True)
707+
self.assertEqual(realpath(ABSTFN + "/.", strict=False), ABSTFN)
708+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=True)
709+
self.assertEqual(realpath(ABSTFN + "/..", strict=False), dirname(ABSTFN))
710+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=True)
711+
self.assertEqual(realpath(ABSTFN + "/subdir", strict=False), ABSTFN + "/subdir")
712+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=True)
713+
finally:
714+
os_helper.unlink(ABSTFN)
715+
716+
@os_helper.skip_unless_symlink
717+
@skip_if_ABSTFN_contains_backslash
718+
def test_realpath_nonterminal_symlink_to_file(self):
719+
try:
720+
with open(ABSTFN + "1", 'w') as f:
721+
f.write('test_posixpath wuz ere')
722+
os.symlink(ABSTFN + "1", ABSTFN)
723+
self.assertEqual(realpath(ABSTFN, strict=False), ABSTFN + "1")
724+
self.assertEqual(realpath(ABSTFN, strict=True), ABSTFN + "1")
725+
self.assertEqual(realpath(ABSTFN + "/", strict=False), ABSTFN + "1")
726+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=True)
727+
self.assertEqual(realpath(ABSTFN + "/.", strict=False), ABSTFN + "1")
728+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=True)
729+
self.assertEqual(realpath(ABSTFN + "/..", strict=False), dirname(ABSTFN))
730+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=True)
731+
self.assertEqual(realpath(ABSTFN + "/subdir", strict=False), ABSTFN + "1/subdir")
732+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=True)
733+
finally:
734+
os_helper.unlink(ABSTFN)
735+
736+
@os_helper.skip_unless_symlink
737+
@skip_if_ABSTFN_contains_backslash
738+
def test_realpath_nonterminal_symlink_to_symlinks_to_file(self):
739+
try:
740+
with open(ABSTFN + "2", 'w') as f:
741+
f.write('test_posixpath wuz ere')
742+
os.symlink(ABSTFN + "2", ABSTFN + "1")
743+
os.symlink(ABSTFN + "1", ABSTFN)
744+
self.assertEqual(realpath(ABSTFN, strict=False), ABSTFN + "2")
745+
self.assertEqual(realpath(ABSTFN, strict=True), ABSTFN + "2")
746+
self.assertEqual(realpath(ABSTFN + "/", strict=False), ABSTFN + "2")
747+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/", strict=True)
748+
self.assertEqual(realpath(ABSTFN + "/.", strict=False), ABSTFN + "2")
749+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/.", strict=True)
750+
self.assertEqual(realpath(ABSTFN + "/..", strict=False), dirname(ABSTFN))
751+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/..", strict=True)
752+
self.assertEqual(realpath(ABSTFN + "/subdir", strict=False), ABSTFN + "2/subdir")
753+
self.assertRaises(NotADirectoryError, realpath, ABSTFN + "/subdir", strict=True)
754+
finally:
755+
os_helper.unlink(ABSTFN)
756+
698757
def test_relpath(self):
699758
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
700759
try:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`!posixpath.realpath` now raises :exc:`NotADirectoryError` when *strict*
2+
mode is enabled and a non-directory path with a trailing slash is supplied.

0 commit comments

Comments
 (0)