Skip to content

Commit 5cf1221

Browse files
committed
sysfs: always sanitize values
1 parent c23b7c1 commit 5cf1221

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

sasutils/cli/sas_discover.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@
3232
def format_attrs(attrlist, attrs):
3333
"""filter keys to avoid SysfsObject cache miss on all attrs"""
3434
attr_fmt = ('%s: {%s}' % t for t in attrlist)
35-
# sanitize values for display
36-
iargs = dict((k, attrs.get(k, 'N/A') \
37-
.strip('\x00') \
38-
.encode('ascii', errors='replace') \
39-
.decode()) for _, k in attrlist)
35+
iargs = dict((k, attrs.get(k, 'N/A')) for _, k in attrlist)
4036
return ', '.join(attr_fmt).format(**iargs)
4137

4238

@@ -199,10 +195,7 @@ def __str__(self):
199195
'version_fw')
200196

201197
# sanitize values for display
202-
iargs = dict((k, self.baseobj.scsi_host.attrs.get(k, 'N/A') \
203-
.strip('\x00') \
204-
.encode('ascii', errors='replace') \
205-
.decode())
198+
iargs = dict((k, self.baseobj.scsi_host.attrs.get(k, 'N/A'))
206199
for k in ikeys)
207200

208201
return '%s %s' % (self.name, ', '.join(info_fmt).format(**iargs))

sasutils/sysfs.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929
SYSFS_ROOT = '/sys'
3030

31+
# Some VPDs contain weird characters...
32+
def sanitize_sysfs_value(value):
33+
return value.strip('\x00').encode('ascii', errors='replace').decode()
34+
3135

3236
class SysfsNode(object):
3337
def __init__(self, path=None):
@@ -121,7 +125,7 @@ def get(self, pathname, default=None, ignore_errors=False, printable=True,
121125
raise KeyError('Not found: %s' % path)
122126
result = default
123127

124-
return result
128+
return sanitize_sysfs_value(result)
125129

126130
def readlink(self, pathname, default=None, absolute=False):
127131
if absolute:
@@ -177,7 +181,7 @@ def load(self):
177181
# The next five methods are requirements of the ABC.
178182

179183
def __setitem__(self, key, value):
180-
self.values[key] = value
184+
self.values[key] = sanitize_sysfs_value(value)
181185

182186
def get(self, key, default=None):
183187
if not self.values.__contains__(key):
@@ -189,6 +193,7 @@ def get(self, key, default=None):
189193
else:
190194
raise AttributeError("%r object has no attribute %r" %
191195
(self.__class__.__name__, key))
196+
192197
return self.values[key]
193198

194199
def __getitem__(self, key):

0 commit comments

Comments
 (0)