Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn main() -> Result<()> {
return list_devices(&args.transport);
}

let payload = gnerate_payload(&mut args)?;
let payload = generate_payload(&mut args)?;

write_payload(&args.transport, payload)
}
Expand All @@ -129,7 +129,7 @@ fn list_devices(transport: &TransportProtocol) -> Result<()> {
Ok(())
}

fn gnerate_payload(args: &mut Args) -> Result<PayloadBuffer> {
fn generate_payload(args: &mut Args) -> Result<PayloadBuffer> {
let config_path = args.config.take().unwrap_or_default();
let config = fs::read_to_string(&config_path)
.with_context(|| format!("load config: {config_path:?}"))?;
Expand Down
33 changes: 30 additions & 3 deletions src/usb_hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,40 @@ fn write_raw(device: &HidDevice, data: &[u8]) -> Result<()> {
// just to be sure
assert!(data.len() <= 8192);

let n = device.write(data).context("write payload")?;
let mut written: usize = 0;

#[cfg(windows)]
{
written = 0;

while written < data.len() {
let new_data: &[u8] = &prepend_byte_and_offset(data, written);
let n = device.write(new_data).context("write payload")?;
written = written + n - 1;
Comment on lines +107 to +110
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential off-by-one error in written byte calculation.

Subtracting 1 from 'written + n' may cause incorrect loop termination or skipped bytes, particularly when 'n' is 0 or 1. Please verify if this adjustment is needed.

}
}

#[cfg(not(windows))]
{
written = device.write(data).context("write payload")?;
}

anyhow::ensure!(
n == data.len(),
"incomplete write: {n} of {} bytes",
written == data.len(),
"incomplete write: {written} of {} bytes",
data.len()
);

Ok(())
}

fn prepend_byte_and_offset(data: &[u8], offset: usize) -> [u8; 65] {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Possible out-of-bounds panic if offset > data.len().

Add a debug assertion or explicit check to ensure offset does not exceed data.len().

let mut result: [u8; 65] = [0u8; 65];
result[1] = 0x0;
if data.len() - offset < 64 {
result[1..].copy_from_slice(&data[offset..]);
} else {
result[1..].copy_from_slice(&data[offset..offset + 64]);
}
result
}