-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
c232ebc
200428d
914f1f9
9e66a19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,13 +98,36 @@ 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); | ||
sourcery-ai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let n = device.write(new_data).context("write payload")?; | ||
written = written + n - 1; | ||
Comment on lines
+107
to
+110
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
result[1..].copy_from_slice(&data[offset..offset + 64]); | ||
result | ||
} |
Uh oh!
There was an error while loading. Please reload this page.