Skip to content

Commit 158db11

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.exists, os.path.ismount, os.fspath, os.path.islink
1 parent 6150bb2 commit 158db11

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Lib/posixpath.py

+1-1
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

+9
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,15 @@ def test_realpath_resolve_first(self):
646646
safe_rmdir(ABSTFN + "/k")
647647
safe_rmdir(ABSTFN)
648648

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

0 commit comments

Comments
 (0)