Skip to content

Commit 402201e

Browse files
committed
Merge branch 'handle-panics'
2 parents fd7f793 + 7265e60 commit 402201e

File tree

8 files changed

+29
-13
lines changed

8 files changed

+29
-13
lines changed

src/rust/bitbox02-rust-c/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod workflow;
4747
fn panic(info: &core::panic::PanicInfo) -> ! {
4848
::util::log::log!("{}", info);
4949
#[cfg(feature = "firmware")]
50-
bitbox02_rust::print_debug!(0, "Error: {}", info);
50+
bitbox02_rust::print_screen!(0, "Error: {}", info);
5151
loop {}
5252
}
5353

src/rust/bitbox02-rust/src/general.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@
1414

1515
#[macro_use]
1616
pub mod screen;
17+
18+
/// displays the input error message on the screen and enters
19+
/// an infinite loop.
20+
#[allow(clippy::empty_loop)]
21+
pub fn abort(err: &str) -> ! {
22+
print_screen!(0, "Error: {}", err);
23+
loop {}
24+
}

src/rust/bitbox02-rust/src/general/screen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ pub fn print_debug_internal(duration: Duration, msg: &str) {
3131
/// ```no_run
3232
/// # #[macro_use] extern crate bitbox02_rust; fn main() {
3333
/// let my_str = "abc";
34-
/// print_debug!(1000, "{}", &my_str);
34+
/// print_screen!(1000, "{}", &my_str);
3535
/// # }
3636
/// ```
3737
#[macro_export]
38-
macro_rules! print_debug {
38+
macro_rules! print_screen {
3939
($duration:expr, $($arg:tt)*) => ({
4040
extern crate alloc;
4141
let duration = core::time::Duration::from_millis($duration);

src/rust/bitbox02-rust/src/hww/api/restore.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::pb;
1717

1818
use pb::response::Response;
1919

20+
use crate::general::abort;
2021
use crate::workflow::{confirm, mnemonic, password, status, unlock};
2122

2223
pub async fn from_file(request: &pb::RestoreBackupRequest) -> Result<Response, Error> {
@@ -75,7 +76,9 @@ pub async fn from_file(request: &pb::RestoreBackupRequest) -> Result<Response, E
7576
}
7677

7778
bitbox02::memory::set_initialized().or(Err(Error::Memory))?;
78-
bitbox02::keystore::unlock(&password).expect("restore_from_file: unlock failed");
79+
if bitbox02::keystore::unlock(&password).is_err() {
80+
abort("restore_from_file: unlock failed");
81+
};
7982

8083
// Ignore non-critical error.
8184
let _ = bitbox02::memory::set_device_name(&metadata.name);
@@ -144,9 +147,11 @@ pub async fn from_mnemonic(
144147
}
145148

146149
bitbox02::memory::set_initialized().or(Err(Error::Memory))?;
147-
148150
// This should never fail.
149-
bitbox02::keystore::unlock(&password).expect("restore_from_mnemonic: unlock failed");
151+
if bitbox02::keystore::unlock(&password).is_err() {
152+
abort("restore_from_mnemonic: unlock failed");
153+
};
154+
150155
unlock::unlock_bip39().await;
151156
Ok(Response::Success(pb::Success {}))
152157
}

src/rust/bitbox02-rust/src/workflow/unlock.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use crate::general::abort;
1516
use crate::workflow::confirm;
1617
use crate::workflow::password;
1718
use crate::workflow::status::status;
@@ -116,9 +117,10 @@ pub async fn unlock_bip39() {
116117
}
117118
}
118119

119-
bitbox02::ui::with_lock_animation(|| {
120-
keystore::unlock_bip39(&mnemonic_passphrase).expect("bip39 unlock failed");
121-
});
120+
let result = bitbox02::ui::with_lock_animation(|| keystore::unlock_bip39(&mnemonic_passphrase));
121+
if result.is_err() {
122+
abort("bip39 unlock failed");
123+
}
122124
}
123125

124126
/// Invokes the unlock workflow. This function does not finish until the keystore is unlocked, or

src/rust/bitbox02/src/ui/ui.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,11 @@ pub fn trinary_input_string_set_input(component: &mut Component, word: &str) {
422422
}
423423
}
424424

425-
pub fn with_lock_animation<F: Fn()>(f: F) {
425+
pub fn with_lock_animation<F: Fn() -> R, R>(f: F) -> R {
426426
unsafe { bitbox02_sys::lock_animation_start() };
427-
f();
427+
let result = f();
428428
unsafe { bitbox02_sys::lock_animation_stop() };
429+
result
429430
}
430431

431432
pub fn screen_stack_pop_all() {

src/rust/bitbox02/src/ui/ui_stub.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn trinary_input_string_set_input(_component: &mut Component, _word: &str) {
151151
panic!("not implemented")
152152
}
153153

154-
pub fn with_lock_animation<F: Fn()>(f: F) {
154+
pub fn with_lock_animation<F: Fn() -> R, R>(f: F) -> R {
155155
f()
156156
}
157157

src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn trinary_input_string_set_input(_component: &mut Component, _word: &str) {
162162
panic!("not implemented")
163163
}
164164

165-
pub fn with_lock_animation<F: Fn()>(f: F) {
165+
pub fn with_lock_animation<F: Fn() -> R, R>(f: F) -> R {
166166
f()
167167
}
168168

0 commit comments

Comments
 (0)