Skip to content

Commit

Permalink
#717: ignore everything after the first occurrence of '\x00' instead …
Browse files Browse the repository at this point in the history
…of replacing '\x00' for the whole string
  • Loading branch information
giampaolo committed Dec 15, 2015
1 parent 29334f1 commit 9e3453c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions psutil/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
long = int
xrange = range
unicode = str
basestring = str

def u(s):
return s
Expand All @@ -27,6 +28,7 @@ def b(s):
long = long
xrange = xrange
unicode = unicode
basestring = basestring

def u(s):
return unicode(s, "unicode_escape")
Expand Down
16 changes: 11 additions & 5 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from ._common import supports_ipv6
from ._common import usage_percent
from ._compat import b
from ._compat import basestring
from ._compat import long
from ._compat import PY3

Expand Down Expand Up @@ -152,14 +153,19 @@ def get_procfs_path():

def readlink(path):
"""Wrapper around os.readlink()."""
assert isinstance(path, basestring), path
path = os.readlink(path)
# readlink() might return paths containing null bytes causing
# problems when used with other fs-related functions (os.*,
# open(), ...), see:
# readlink() might return paths containing null bytes ('\x00')
# resulting in "TypeError: must be encoded string without NULL
# bytes, not str" errors when the string is passed to other
# fs-related functions (os.*, open(), ...).
# Apparently everything after '\x00' is garbage (we can have
# ' (deleted)', 'new' and possibly others), see:
# https://github.com/giampaolo/psutil/issues/717
path = path.replace('\x00', '')
path = path.split('\x00')[0]
# Certain paths have ' (deleted)' appended. Usually this is
# bogus as the file actually exists.
# bogus as the file actually exists. Even if it doesn't we
# don't care.
if path.endswith(' (deleted)') and not path_exists_strict(path):
path = path[:-10]
return path
Expand Down

0 comments on commit 9e3453c

Please sign in to comment.