p2p: create Arbitrary `CommandString` from a buffer of ASCII chars
What changed, and why it matters
This is a small performance improvement for fuzz testing code. It changes how random Bitcoin P2P command strings are generated during fuzzing, avoiding a temporary String allocation. There is no security issue visible in the change.
No security action required. This is a benign fuzzing-only optimization.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies the Arbitrary implementation for CommandString (gated behind the arbitrary feature) to fill a fixed 12-byte buffer directly with ASCII bytes instead of allocating a String and converting it. The generated bytes are constrained to 1..127 (ASCII non-null), and the buffer is pre-zeroed so any unfilled tail remains null padding. This is functionally equivalent to the prior behavior for the purposes of fuzzing and does not affect production code paths.
Changed components
p2p/src/message.rsCommandString Arbitrary implementationInspect captured patch +9 / −2
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index 8dc34c88..57ddc018 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -2248,8 +2248,15 @@ impl<'a> Arbitrary<'a> for InventoryPayload {
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for CommandString {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
- let s = u.arbitrary::<String>()?;
- Self::try_from(s).map_err(|_| arbitrary::Error::IncorrectFormat)
+ let mut buf = [0; Self::MAX_LEN];
+ let mut buf_iter = buf.iter_mut();
+
+ // ascii `0` pads end of command.
+ while let (Some(dest), ascii @ 1..) = (buf_iter.next(), u8::arbitrary(u)? % 128) {
+ *dest = ascii;
+ }
+
+ Ok(Self(buf))
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.