refactor(core/rust): implement io::Error
What changed, and why it matters
This is a small internal code cleanup in the Trezor firmware's Rust code. It introduces a dedicated Rust error type for input/output operations so the code no longer borrows MicroPython's error type directly. The behavior appears unchanged: the same end-of-buffer condition still returns an equivalent EOF error, and the bytes-type check still returns the same type error. There is no indication this fixes or introduces a security vulnerability.
No security action required. Treat as normal code-quality refactor during routine review.
Security signals we found
No security-relevant behavioral change visible in the diff
Error-handling refactor only
No bounds-check removal or weakening
No new unsafe code or external inputs introduced
Evidence from the diff
The commit refactors core/embed/rust/src/io.rs to define a local io::Error enum (currently just EOFError) and implements From<io::Error> for crate::micropython::error::Error. It updates BinaryData<'static>::try_from(Obj) to use crate::micropython::error::Error as its Error type explicitly. In protobuf/encode.rs, BufferStream now returns Error::EOFError directly instead of calling error::end_of_buffer(). The diff is purely structural; error semantics are preserved.
Changed components
core/embed/rust/src/io.rscore/embed/rust/src/protobuf/encode.rsInspect captured patch +17 / −5
### core/embed/rust/src/io.rs
@@ -1,7 +1,19 @@
-use crate::error::Error;
#[cfg(feature = "micropython")]
use crate::micropython::{buffer::get_buffer, gc::Gc, obj::Obj};
+pub enum Error {
+ EOFError,
+}
+
+#[cfg(feature = "micropython")]
+impl From<Error> for crate::micropython::error::Error {
+ fn from(error: Error) -> Self {
+ match error {
+ Error::EOFError => crate::micropython::error::Error::EOFError,
+ }
+ }
+}
+
pub struct InputStream<'a> {
buf: &'a [u8],
pos: usize,
@@ -161,11 +173,11 @@ impl From<Gc<[u8]>> for BinaryData<'static> {
#[cfg(feature = "micropython")]
impl TryFrom<Obj> for BinaryData<'static> {
- type Error = Error;
+ type Error = crate::micropython::error::Error;
fn try_from(obj: Obj) -> Result<Self, Self::Error> {
if !obj.is_bytes() {
- return Err(Error::TypeError);
+ return Err(crate::micropython::error::Error::TypeError);
}
Ok(Self::Object(obj))
}
### core/embed/rust/src/protobuf/encode.rs
@@ -207,7 +207,7 @@ impl<'a> OutputStream for BufferStream<'a> {
*pos += len;
buf.copy_from_slice(val);
})
- .ok_or_else(error::end_of_buffer)
+ .ok_or(Error::EOFError)
}
fn write_byte(&mut self, val: u8) -> Result<(), Error> {
@@ -218,6 +218,6 @@ impl<'a> OutputStream for BufferStream<'a> {
*pos += 1;
*buf = val;
})
- .ok_or_else(error::end_of_buffer)
+ .ok_or(Error::EOFError)
}
}Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.