Skip to content

Commit

Permalink
Fix realpath ValueError on null byte
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stefanhoelzl committed Apr 5, 2024
1 parent 6150bb2 commit 2155d32
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def realpath(filename, *, strict=False):
if not stat.S_ISLNK(st.st_mode):
path = newpath
continue
except OSError:
except (OSError, ValueError):
if strict:
raise
path = newpath
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@ def test_realpath_resolve_first(self):
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)

def test_realpath_null_byte(self):
path_with_null_byte = ABSTFN + "/\x00"
try:
os.mkdir(ABSTFN)
self.assertEqual(realpath(path_with_null_byte), path_with_null_byte)
finally:
safe_rmdir(ABSTFN)

def test_relpath(self):
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
try:
Expand Down

0 comments on commit 2155d32

Please sign in to comment.