Skip to content

Commit 2155d32

Browse files
committed
Fix realpath ValueError on null byte
Calling os.path.realpath with a path containg a null byte ("\x00") it raised an ValueError on posix systems. This fix will catch the ValueError similar to how other path operations are handling such an Error. e.g. os.path.existsm os.path.ismount, os.fspath, os.path.islink
1 parent 6150bb2 commit 2155d32

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Lib/posixpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def realpath(filename, *, strict=False):
460460
if not stat.S_ISLNK(st.st_mode):
461461
path = newpath
462462
continue
463-
except OSError:
463+
except (OSError, ValueError):
464464
if strict:
465465
raise
466466
path = newpath

Lib/test/test_posixpath.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,14 @@ def test_realpath_resolve_first(self):
646646
safe_rmdir(ABSTFN + "/k")
647647
safe_rmdir(ABSTFN)
648648

649+
def test_realpath_null_byte(self):
650+
path_with_null_byte = ABSTFN + "/\x00"
651+
try:
652+
os.mkdir(ABSTFN)
653+
self.assertEqual(realpath(path_with_null_byte), path_with_null_byte)
654+
finally:
655+
safe_rmdir(ABSTFN)
656+
649657
def test_relpath(self):
650658
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
651659
try:

0 commit comments

Comments
 (0)