Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make array access a bit safer #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ async fn serprog_task(mut class: CdcAcmClass<'static, CustomUsbDriver>, r: SpiRe
log::error!("Error reading packet: {:?}", e);
continue;
}
match SerprogCommand::from(buf[0]) {
match SerprogCommand::from(buf.get(0).copied().unwrap_or(0)) {
SerprogCommand::Nop => {
log::debug!("Received Nop CMD");
if let Err(e) = class.write_packet(&[S_ACK]).await {
Expand Down Expand Up @@ -363,7 +363,7 @@ async fn serprog_task(mut class: CdcAcmClass<'static, CustomUsbDriver>, r: SpiRe
log::error!("Error reading packet: {:?}", e);
continue;
}
if buf[0] == 0x08 {
if buf.get(0).copied().unwrap_or(0) == 0x08 {
log::debug!("Received SBustype 'SPI'");
if let Err(e) = class.write_packet(&[S_ACK]).await {
log::error!("Error writing packet: {:?}", e);
Expand All @@ -381,8 +381,18 @@ async fn serprog_task(mut class: CdcAcmClass<'static, CustomUsbDriver>, r: SpiRe
log::error!("Error reading packet: {:?}", e);
continue;
}
let op_slen = u32::from_le_bytes([buf[0], buf[1], buf[2], 0]);
let op_rlen = u32::from_le_bytes([buf[3], buf[4], buf[5], 0]);
let op_slen = u32::from_le_bytes([
buf.get(0).copied().unwrap_or(0),
buf.get(1).copied().unwrap_or(0),
buf.get(2).copied().unwrap_or(0),
0,
]);
let op_rlen = u32::from_le_bytes([
buf.get(3).copied().unwrap_or(0),
buf.get(4).copied().unwrap_or(0),
buf.get(5).copied().unwrap_or(0),
0,
]);

let mut sdata = [0_u8; MAX_BUFFER_SIZE];
let mut rdata = [0_u8; MAX_BUFFER_SIZE];
Expand Down