From ab8b04ad30da9fa2678bb081b7e19ed1bd0e001e Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 9 Dec 2022 16:11:21 +0100 Subject: [PATCH] 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 --- src/python/devtest/os/Connection.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/python/devtest/os/Connection.py b/src/python/devtest/os/Connection.py index d35ada2..260e2eb 100644 --- a/src/python/devtest/os/Connection.py +++ b/src/python/devtest/os/Connection.py @@ -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]