Skip to content

Commit

Permalink
Fix static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
agrojean-ledger committed Nov 24, 2023
1 parent 69ef4c2 commit bb72d74
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/app_ui/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, AppSW> {
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)?;

Expand Down
9 changes: 5 additions & 4 deletions src/app_ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ fn ui_about_menu(comm: &mut Comm) -> Event<ApduHeader> {
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);
}
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/app_ui/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ pub fn ui_display_tx(tx: &Tx) -> Result<bool, AppSW> {
.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);
let hex_addr_str_with_prefix =
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 = [
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/get_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/handlers/sign_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
27 changes: 12 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ enum Ins {
GetAppName,
GetPubkey,
SignTx,
UnknownIns,
Unknown,
}

impl From<ApduHeader> for Ins {
Expand All @@ -96,7 +96,7 @@ impl From<ApduHeader> for Ins {
4 => Ins::GetAppName,
5 => Ins::GetPubkey,
6 => Ins::SignTx,
_ => Ins::UnknownIns,
_ => Ins::Unknown,
}
}
}
Expand All @@ -107,16 +107,14 @@ extern "C" fn sample_pending() {

loop {
ui::SingleMessage::new("Pending").show();
match comm.next_event::<Ins>() {
Event::Button(ButtonEvent::RightButtonRelease) => break,
_ => (),
if let Event::Button(ButtonEvent::RightButtonRelease) = comm.next_event::<Ins>() {
break;
}
}
loop {
ui::SingleMessage::new("Ledger review").show();
match comm.next_event::<Ins>() {
Event::Button(ButtonEvent::BothButtonsRelease) => break,
_ => (),
if let Event::Button(ButtonEvent::BothButtonsRelease) = comm.next_event::<Ins>() {
break;
}
}
}
Expand All @@ -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)),
},
_ => (),
}
}
}
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize, AppSW> {
// 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);
}

Expand Down

0 comments on commit bb72d74

Please sign in to comment.