From a9b46f02493ce0a686bfbe8cdda7c6464d812293 Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 11 Apr 2022 13:09:57 +0200 Subject: [PATCH] fix(core/rust): be more defensive about strings coming in from uPy --- core/embed/rust/src/micropython/buffer.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/embed/rust/src/micropython/buffer.rs b/core/embed/rust/src/micropython/buffer.rs index b2c4f151ca..e298e6ca34 100644 --- a/core/embed/rust/src/micropython/buffer.rs +++ b/core/embed/rust/src/micropython/buffer.rs @@ -165,8 +165,11 @@ impl Deref for StrBuffer { impl AsRef for StrBuffer { fn as_ref(&self) -> &str { - // SAFETY: MicroPython ensures that values of type `str` are UTF-8 - unsafe { str::from_utf8_unchecked(self.0.as_ref()) } + // MicroPython _should_ ensure that values of type `str` are UTF-8. + // Rust seems to be stricter in what it considers UTF-8 though. + // In case there's a mismatch, this code will cleanly panic + // before attempting to use the data. + str::from_utf8(self.0.as_ref()).unwrap() } }