From d6ff82034051117b1fa94140a25bb5b46d58f901 Mon Sep 17 00:00:00 2001 From: Alexis Grojean Date: Fri, 24 Nov 2023 15:37:19 +0100 Subject: [PATCH] Fix static analysis --- Cargo.toml | 2 +- src/app_ui/address.rs | 4 ++-- src/app_ui/menu.rs | 9 +++++---- src/app_ui/sign.rs | 4 ++-- src/handlers/get_public_key.rs | 2 +- src/handlers/sign_tx.rs | 1 + src/main.rs | 27 ++++++++++++--------------- src/utils.rs | 2 +- 8 files changed, 25 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d2d8956..b7966e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ pending_review_screen = ["ledger_device_sdk/pending_review_screen"] [package.metadata.ledger] curve = ["secp256k1"] flags = "0" -path = [""] +path = ["44'/1'"] name = "Rust Boilerplate" [package.metadata.ledger.nanos] diff --git a/src/app_ui/address.rs b/src/app_ui/address.rs index 55713f5..28b3b20 100644 --- a/src/app_ui/address.rs +++ b/src/app_ui/address.rs @@ -25,13 +25,13 @@ use ledger_device_ui_sdk::ui::{Field, MultiFieldReview}; const DISPLAY_ADDR_BYTES_LEN: usize = 20; pub fn ui_display_pk(addr: &[u8]) -> Result { - let addr_hex_str_buf = to_hex_all_caps(&addr[addr.len() - DISPLAY_ADDR_BYTES_LEN as usize..]) + let addr_hex_str_buf = to_hex_all_caps(&addr[addr.len() - DISPLAY_ADDR_BYTES_LEN..]) .map_err(|_| AppSW::AddrDisplayFail)?; let addr_hex_str = from_utf8(&addr_hex_str_buf[..DISPLAY_ADDR_BYTES_LEN * 2]) .map_err(|_| AppSW::AddrDisplayFail)?; let mut addr_hex_str_with_prefix_buf = [0u8; DISPLAY_ADDR_BYTES_LEN * 2 + 2]; - concatenate(&["0x", &addr_hex_str], &mut addr_hex_str_with_prefix_buf); + concatenate(&["0x", addr_hex_str], &mut addr_hex_str_with_prefix_buf); let addr_hex_str_with_prefix = from_utf8(&addr_hex_str_with_prefix_buf).map_err(|_| AppSW::AddrDisplayFail)?; diff --git a/src/app_ui/menu.rs b/src/app_ui/menu.rs index ac39c3e..87c95a8 100644 --- a/src/app_ui/menu.rs +++ b/src/app_ui/menu.rs @@ -28,10 +28,11 @@ fn ui_about_menu(comm: &mut Comm) -> Event { loop { match MultiPageMenu::new(comm, &pages).show() { EventOrPageIndex::Event(e) => return e, - i => match i { - EventOrPageIndex::Index(1) => return ui_menu_main(comm), - _ => (), - }, + i => { + if let EventOrPageIndex::Index(1) = i { + return ui_menu_main(comm); + } + } } } } diff --git a/src/app_ui/sign.rs b/src/app_ui/sign.rs index 1531b81..d197815 100644 --- a/src/app_ui/sign.rs +++ b/src/app_ui/sign.rs @@ -36,7 +36,7 @@ pub fn ui_display_tx(tx: &Tx) -> Result { .trim_matches(char::from(0)); // Format destination address - let hex_addr_buf = to_hex_all_caps(&tx.to).map_err(|_| AppSW::TxDisplayFail)?; + let hex_addr_buf = to_hex_all_caps(tx.to).map_err(|_| AppSW::TxDisplayFail)?; let hex_addr_str = from_utf8(&hex_addr_buf).map_err(|_| AppSW::TxDisplayFail)?; let mut addr_with_prefix_buf = [0u8; 42]; concatenate(&["0x", hex_addr_str], &mut addr_with_prefix_buf); @@ -44,7 +44,7 @@ pub fn ui_display_tx(tx: &Tx) -> Result { from_utf8(&addr_with_prefix_buf).map_err(|_| AppSW::TxDisplayFail)?; // Format memo - let memo_str = from_utf8(&tx.memo[..tx.memo_len as usize]).map_err(|_| AppSW::TxDisplayFail)?; + let memo_str = from_utf8(&tx.memo[..tx.memo_len]).map_err(|_| AppSW::TxDisplayFail)?; // Define transaction review fields let my_fields = [ diff --git a/src/handlers/get_public_key.rs b/src/handlers/get_public_key.rs index 7d93ff7..036162a 100644 --- a/src/handlers/get_public_key.rs +++ b/src/handlers/get_public_key.rs @@ -54,7 +54,7 @@ pub fn handler_get_public_key(comm: &mut Comm, display: bool) -> Result<(), AppS &mut keccak256.header as *mut cx_hash_t, CX_LAST, pk_ptr, - 64 as usize, + 64_usize, address.as_mut_ptr(), address.len(), ) != CX_OK diff --git a/src/handlers/sign_tx.rs b/src/handlers/sign_tx.rs index cc38939..f3668c5 100644 --- a/src/handlers/sign_tx.rs +++ b/src/handlers/sign_tx.rs @@ -26,6 +26,7 @@ use ledger_secure_sdk_sys::{ const MAX_TRANSACTION_LEN: usize = 510; pub struct Tx<'a> { + #[allow(dead_code)] nonce: u64, pub value: u64, pub to: &'a [u8], diff --git a/src/main.rs b/src/main.rs index 2966b8d..069bd09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,7 +86,7 @@ enum Ins { GetAppName, GetPubkey, SignTx, - UnknownIns, + Unknown, } impl From for Ins { @@ -96,7 +96,7 @@ impl From for Ins { 4 => Ins::GetAppName, 5 => Ins::GetPubkey, 6 => Ins::SignTx, - _ => Ins::UnknownIns, + _ => Ins::Unknown, } } } @@ -107,16 +107,14 @@ extern "C" fn sample_pending() { loop { ui::SingleMessage::new("Pending").show(); - match comm.next_event::() { - Event::Button(ButtonEvent::RightButtonRelease) => break, - _ => (), + if let Event::Button(ButtonEvent::RightButtonRelease) = comm.next_event::() { + break; } } loop { ui::SingleMessage::new("Ledger review").show(); - match comm.next_event::() { - Event::Button(ButtonEvent::BothButtonsRelease) => break, - _ => (), + if let Event::Button(ButtonEvent::BothButtonsRelease) = comm.next_event::() { + break; } } } @@ -129,12 +127,11 @@ extern "C" fn sample_main() { loop { // Wait for either a specific button push to exit the app // or an APDU command - match ui_menu_main(&mut comm) { - Event::Command(ins) => match handle_apdu(&mut comm, ins.into(), &mut tx_ctx) { + if let Event::Command(ins) = ui_menu_main(&mut comm) { + match handle_apdu(&mut comm, ins.into(), &mut tx_ctx) { Ok(()) => comm.reply_ok(), Err(sw) => comm.reply(Reply::from(sw)), - }, - _ => (), + } } } } @@ -170,7 +167,7 @@ fn handle_apdu(comm: &mut Comm, ins: Ins, ctx: &mut TxContext) -> Result<(), App match comm.get_data() { Ok(data) => { - if data.len() == 0 { + if data.is_empty() { return Err(AppSW::WrongDataLength); } } @@ -189,7 +186,7 @@ fn handle_apdu(comm: &mut Comm, ins: Ins, ctx: &mut TxContext) -> Result<(), App match comm.get_data() { Ok(data) => { - if data.len() == 0 { + if data.is_empty() { return Err(AppSW::WrongDataLength); } } @@ -203,7 +200,7 @@ fn handle_apdu(comm: &mut Comm, ins: Ins, ctx: &mut TxContext) -> Result<(), App ctx, ); } - Ins::UnknownIns => { + Ins::Unknown => { return Err(AppSW::InsNotSupported); } } diff --git a/src/utils.rs b/src/utils.rs index a5ce270..59a72c2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -40,7 +40,7 @@ pub fn to_hex_all_caps(m: &[u8]) -> Result<[u8; MAX_HEX_LEN], ()> { /// Convert serialized derivation path to u32 array elements pub fn read_bip32_path(data: &[u8], path: &mut [u32]) -> Result { // Check input length and path buffer capacity - if data.len() < 1 || path.len() < data.len() / 4 { + if data.is_empty() || path.len() < data.len() / 4 { return Err(AppSW::WrongDataLength); }