os.Connection._read(): Safeguard against non-utf8

Don't try to decode non-UTF-8 garbage as UTF-8 if it shows up in _read().

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2022-12-09 16:11:21 +01:00
commit ab8b04ad30

View file

@ -120,7 +120,11 @@ class Connection(ABC): # export
r = await self._read(act_timeout, flags)
if r is not None:
if flags & self.Flags.ReadDecodeToString:
r = r.decode('utf-8')
try:
r = r.decode('utf-8', errors='replace')
except Exception as e:
slog(WARNING, 'Failed to decode string, returning undecoded ({}): "{}"'.format(e, r))
return str(r)
if flags & self.Flags.ReadStripNewline:
if len(r) and r[-1] == '\n':
r = r[0:-1]