Skip to content

Commit

Permalink
Use ArrayVec::push_many_from_slice to make code safer
Browse files Browse the repository at this point in the history
We were ignoring potential (even though it shouldn't happen) failure.
The new way however is correct by construction.

The `ArrayVec` method is from bluss/arrayvec#237
  • Loading branch information
Ericson2314 committed Apr 11, 2023
1 parent 56c1e97 commit d8fa38c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -13,6 +13,9 @@ nanos_sdk = { git = "https://github.com/LedgerHQ/ledger-nanos-sdk.git" }
nanos_ui = { git = "https://github.com/LedgerHQ/ledger-nanos-ui.git" }
ledger-parser-combinators = { git = "https://github.com/alamgu/ledger-parser-combinators", branch="async-split-take-2" }

[patch."crates-io".arrayvec]
git = "https://github.com/obsidiansystems/arrayvec"
branch = "push-many-from-slice"
[patch."https://github.com/LedgerHQ/ledger-nanos-sdk.git".nanos_sdk]
git = "https://github.com/alamgu/ledger-nanos-sdk.git"
branch = "relocating-loader-w-fixes"
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -363,8 +363,8 @@ impl Readable for ByteStream {
Err(_) => reject().await,
};
let avail = &chunk[self.current_offset + HASH_LEN..];
let consuming = core::cmp::min(avail.len(), buffer.remaining_capacity());
buffer.try_extend_from_slice(&avail[0..consuming]).ok();
let remaining = buffer.push_many_from_slice(&avail).len();
let consuming = avail.len() - remaining;
self.current_offset += consuming;
if self.current_offset + HASH_LEN == chunk.len() {
self.current_chunk = chunk[0..HASH_LEN].try_into().unwrap();
Expand Down

0 comments on commit d8fa38c

Please sign in to comment.