1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-26 07:11:25 +00:00

fix(core/rust): be more defensive about strings coming in from uPy

This commit is contained in:
matejcik 2022-04-11 13:09:57 +02:00 committed by matejcik
parent 8dd7a22f8b
commit a9b46f0249

View File

@ -165,8 +165,11 @@ impl Deref for StrBuffer {
impl AsRef<str> 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()
}
}