refactor(dpi/ftp): move owned Vec<u8> into String, drop redundant copy#327
Open
obchain wants to merge 1 commit into
Open
refactor(dpi/ftp): move owned Vec<u8> into String, drop redundant copy#327obchain wants to merge 1 commit into
Vec<u8> into String, drop redundant copy#327obchain wants to merge 1 commit into
Conversation
`analyze_ftp` built the FTP command string from an already-owned
`Vec<u8>` via:
let command = std::str::from_utf8(&upper).ok()?.to_string();
`std::str::from_utf8` borrows the `Vec`'s bytes and returns a `&str`;
`.to_string()` then allocates a fresh `String` and copies those bytes
in. `upper` is dropped on the next statement, so the copy is purely
shape-driven — nothing else reads the `Vec`.
`first_token_upper` (line 169) already returns early with `Vec::new()`
unless every byte in the token passes `b.is_ascii_alphabetic()`, so
the bytes are valid UTF-8 by construction. Take the `Vec` by value
with `String::from_utf8` to skip the redundant copy. The `?` stays as
a defensive guard against a future widening of `first_token_upper`'s
accepted byte range, and the new comment records the invariant the
guard rests on.
Behavior is unchanged.
Refs domcyrus#326
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
analyze_ftp(src/network/dpi/ftp.rs) built the FTP command string from an already-ownedVec<u8>via:std::str::from_utf8borrows theVec's bytes and returns a&str;.to_string()then allocates a freshStringand copies those bytes in.upperis dropped on the next statement, so the copy is purely shape-driven — nothing else reads theVec.first_token_upper(line 169) already returns early withVec::new()unless every byte in the token passesb.is_ascii_alphabetic(), so the bytes are valid UTF-8 by construction. Take theVecby value withString::from_utf8to skip the redundant copy:The
?stays as a defensive guard against a future widening offirst_token_upper's accepted byte range, and the new comment records the invariant the guard rests on.Behavior is unchanged.
Closes #326.
Verification
cargo test --lib network::dpi::ftp: 13 passed, 0 failed.cargo test --lib: all pass.cargo clippy --all-targets -- -D warnings: clean.cargo fmt --check: clean.Notes
Fits the recent
refactor(dpi/...)allocation-cleanup series (#290, #301, #307, #317) — single-line change, no behavioural impact, removes oneStringallocation + memcpy per FTP command request packet.