1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-03-28 22:15:42 +00:00

refactor(core/rust): replace panic! by fatal_error!

[no changelog]
This commit is contained in:
cepetr 2024-06-11 14:59:16 +02:00 committed by cepetr
parent f5203011c5
commit 06faae8f82
17 changed files with 29 additions and 31 deletions

View File

@ -73,7 +73,7 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {
// raises a Hard Fault on hardware. // raises a Hard Fault on hardware.
// //
// Otherwise, use `unwrap!` macro from trezorhal. // Otherwise, use `unwrap!` macro from trezorhal.
fatal_error!("", "rs"); fatal_error!("rs");
} }
#[cfg(not(target_arch = "arm"))] #[cfg(not(target_arch = "arm"))]

View File

@ -87,7 +87,7 @@ impl<'a> Iterator for Iter<'a> {
self.iter_buf.caught_exception = exc; self.iter_buf.caught_exception = exc;
None None
} }
Err(_) => panic!("unexpected error"), Err(_) => fatal_error!("Unexpected error"),
Ok(item) if item == Obj::const_stop_iteration() => { Ok(item) if item == Obj::const_stop_iteration() => {
self.finished = true; self.finished = true;
None None

View File

@ -108,19 +108,19 @@ macro_rules! unwrap {
macro_rules! ensure { macro_rules! ensure {
($what:expr, $error:expr) => { ($what:expr, $error:expr) => {
if !($what) { if !($what) {
fatal_error!(stringify!($what), $error); crate::trezorhal::fatal_error::__fatal_error(
stringify!($what),
$error,
file!(),
line!(),
function_name!(),
);
} }
}; };
} }
macro_rules! fatal_error { macro_rules! fatal_error {
($expr:expr, $msg:expr) => {{ ($msg:expr) => {{
crate::trezorhal::fatal_error::__fatal_error( crate::trezorhal::fatal_error::__fatal_error("", $msg, file!(), line!(), function_name!());
stringify!($expr),
$msg,
file!(),
line!(),
function_name!(),
);
}}; }};
} }

View File

@ -7,7 +7,7 @@ pub unsafe fn get_blob<'a>() -> &'a [u8] {
let mut len: u32 = 0; let mut len: u32 = 0;
let ptr = unsafe { ffi::translations_read(&mut len, 0) }; let ptr = unsafe { ffi::translations_read(&mut len, 0) };
if ptr.is_null() { if ptr.is_null() {
fatal_error!("Translations read failed", ""); fatal_error!("Translations read failed");
} }
// SAFETY: The pointer is always valid. // SAFETY: The pointer is always valid.
unsafe { core::slice::from_raw_parts(ptr, len as usize) } unsafe { core::slice::from_raw_parts(ptr, len as usize) }

View File

@ -57,7 +57,7 @@ impl<T> Animation<T> {
} else { } else {
// Duration is too large to be added to an `Instant`. // Duration is too large to be added to an `Instant`.
#[cfg(feature = "ui_debug")] #[cfg(feature = "ui_debug")]
panic!("offset is too large"); fatal_error!("Offset is too large");
} }
} }

View File

@ -226,7 +226,7 @@ impl<T> Root<T> {
if let Some(ref mut c) = self.inner { if let Some(ref mut c) = self.inner {
c c
} else { } else {
fatal_error!("deallocated", "Root object is deallocated") fatal_error!("Root object is deallocated")
} }
} }
@ -234,7 +234,7 @@ impl<T> Root<T> {
if let Some(ref c) = self.inner { if let Some(ref c) = self.inner {
c c
} else { } else {
fatal_error!("deallocated", "Root object is deallocated") fatal_error!("Root object is deallocated")
} }
} }
@ -459,7 +459,7 @@ where
// Messages raised during a `RequestPaint` dispatch are not propagated, let's // Messages raised during a `RequestPaint` dispatch are not propagated, let's
// make sure we don't do that. // make sure we don't do that.
#[cfg(feature = "ui_debug")] #[cfg(feature = "ui_debug")]
panic!("cannot raise messages during RequestPaint"); fatal_error!("Cannot raise messages during RequestPaint");
} }
// Make sure to at least a propagate the paint flag upwards (in case there are // Make sure to at least a propagate the paint flag upwards (in case there are
// no `Child` instances in `self`, paint would not get automatically requested // no `Child` instances in `self`, paint would not get automatically requested
@ -662,7 +662,7 @@ impl EventCtx {
// The timer queue is full, this would be a development error in the layout // The timer queue is full, this would be a development error in the layout
// layer. Let's panic in the debug env. // layer. Let's panic in the debug env.
#[cfg(feature = "ui_debug")] #[cfg(feature = "ui_debug")]
panic!("timer queue is full"); fatal_error!("Timer queue is full");
} }
} }

View File

@ -163,7 +163,7 @@ where
let mut border = self.border; let mut border = self.border;
let area = match self.align.0 { let area = match self.align.0 {
Alignment::Start => bounds.split_left(self.size.x).0, Alignment::Start => bounds.split_left(self.size.x).0,
Alignment::Center => panic!("alignment not supported"), Alignment::Center => fatal_error!("Alignment not supported"),
Alignment::End => { Alignment::End => {
border.x = -border.x; border.x = -border.x;
bounds.split_right(self.size.x).1 bounds.split_right(self.size.x).1
@ -171,7 +171,7 @@ where
}; };
let area = match self.align.1 { let area = match self.align.1 {
Alignment::Start => area.split_top(self.size.y).0, Alignment::Start => area.split_top(self.size.y).0,
Alignment::Center => panic!("alignment not supported"), Alignment::Center => fatal_error!("Alignment not supported"),
Alignment::End => { Alignment::End => {
border.y = -border.y; border.y = -border.y;
area.split_bottom(self.size.y).1 area.split_bottom(self.size.y).1

View File

@ -766,7 +766,7 @@ impl<'a, const N: usize> VecExt<'a> for Vec<Paragraph<'a>, N> {
} }
if self.push(paragraph).is_err() { if self.push(paragraph).is_err() {
#[cfg(feature = "ui_debug")] #[cfg(feature = "ui_debug")]
panic!("paragraph list is full"); fatal_error!("Paragraph list is full");
} }
self self
} }

View File

@ -47,7 +47,7 @@ impl Glyph {
2 => (width * height + 3) / 4, // packed bits 2 => (width * height + 3) / 4, // packed bits
4 => (width + 1) / 2 * height, // row aligned to bytes 4 => (width + 1) / 2 * height, // row aligned to bytes
8 => width * height, 8 => width * height,
_ => panic!(), _ => fatal_error!("Unsupported font bpp"),
}; };
Glyph { Glyph {

View File

@ -485,7 +485,7 @@ extern "C" fn ui_layout_request_complete_repaint(this: Obj) -> Obj {
// Messages raised during a `RequestPaint` dispatch are not propagated, let's // Messages raised during a `RequestPaint` dispatch are not propagated, let's
// make sure we don't do that. // make sure we don't do that.
#[cfg(feature = "ui_debug")] #[cfg(feature = "ui_debug")]
panic!("cannot raise messages during RequestPaint"); fatal_error!("Cannot raise messages during RequestPaint");
}; };
this.obj_request_clear(); this.obj_request_clear();
Ok(Obj::const_none()) Ok(Obj::const_none())

View File

@ -133,7 +133,7 @@ where
if let Some(word) = self.mnemonic() { if let Some(word) = self.mnemonic() {
word.try_into() word.try_into()
} else { } else {
panic!("invalid mnemonic") fatal_error!("Invalid mnemonic")
} }
} }
MnemonicKeyboardMsg::Previous => "".try_into(), MnemonicKeyboardMsg::Previous => "".try_into(),

View File

@ -49,7 +49,7 @@ impl HoldToConfirm {
ButtonContent::Text(text) => { ButtonContent::Text(text) => {
Self::text(pos, text, LoaderStyleSheet::default_loader(), duration) Self::text(pos, text, LoaderStyleSheet::default_loader(), duration)
} }
ButtonContent::Icon(_) => panic!("Icon is not supported"), ButtonContent::Icon(_) => fatal_error!("Icon is not supported"),
} }
} }

View File

@ -162,7 +162,7 @@ where
if let Some(word) = self.mnemonic() { if let Some(word) = self.mnemonic() {
word.try_into() word.try_into()
} else { } else {
panic!("invalid mnemonic") fatal_error!("Invalid mnemonic")
} }
} }
MnemonicKeyboardMsg::Previous => "".try_into(), MnemonicKeyboardMsg::Previous => "".try_into(),

View File

@ -75,7 +75,7 @@ impl Shape<'_> for Bar {
// is not supported. If we needed it, we would have to // is not supported. If we needed it, we would have to
// introduce a new function in RgbCanvas. // introduce a new function in RgbCanvas.
// TODO: panic! in unsupported scenarious // TODO: fatal_error! in unsupported scenarious
let th = match self.fg_color { let th = match self.fg_color {
Some(_) => self.thickness, Some(_) => self.thickness,

View File

@ -91,7 +91,7 @@ impl Shape<'_> for Circle {
// is not supported. If we needed it, we would have to // is not supported. If we needed it, we would have to
// introduce RgbCanvas::draw_ring() function. // introduce RgbCanvas::draw_ring() function.
// TODO: panic! in unsupported scenarious // TODO: fatal_error! in unsupported scenarious
let th = match self.fg_color { let th = match self.fg_color {
Some(_) => self.thickness, Some(_) => self.thickness,
None => 0, None => 0,

View File

@ -30,7 +30,7 @@ pub struct QrImage {
impl QrImage { impl QrImage {
pub fn new(area: Rect, qrcode: &QrCode) -> Self { pub fn new(area: Rect, qrcode: &QrCode) -> Self {
if area.width() < qrcode.size() as i16 || area.height() < qrcode.size() as i16 { if area.width() < qrcode.size() as i16 || area.height() < qrcode.size() as i16 {
panic!("Too small area"); fatal_error!("Too small area");
} }
let mut result = QrImage { let mut result = QrImage {

View File

@ -22,9 +22,7 @@ impl<T, E> ResultExt for Result<T, E> {
fn assert_if_debugging_ui(self, #[allow(unused)] message: &str) { fn assert_if_debugging_ui(self, #[allow(unused)] message: &str) {
#[cfg(feature = "ui_debug")] #[cfg(feature = "ui_debug")]
if self.is_err() { if self.is_err() {
print!("Panic from assert_if_debugging_ui: "); fatal_error!(message);
println!(message);
panic!("{}", message);
} }
} }
} }