Skip to content

fix: writing data via usb on windows #89

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
36 changes: 33 additions & 3 deletions src/usb_hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,43 @@ 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 offset > data.len() {
return result;
Comment on lines +131 to +132
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Returning a zeroed buffer when offset exceeds data length may mask errors.

Returning a zeroed buffer in this case may hide upstream logic errors. Instead, consider returning an error or panicking to make the issue explicit.

}
if data.len() - offset < 64 {
result[1..].copy_from_slice(&data[offset..]);
} else {
result[1..].copy_from_slice(&data[offset..offset + 64]);
}
result
}