wire: refresh stale decode docs, comments, and fuzz seeds
What changed, and why it matters
This commit is a documentation and test-maintenance cleanup. It corrects outdated code comments that described an old scratch-buffer behavior, adds explanatory comments to benchmark tests, and fixes a fuzz-test bug where reused buffers caused one test seed to silently overwrite another. There are no changes to production logic or security-sensitive behavior.
No security action required. Treat as routine maintenance; optionally review the fuzz-test change to confirm existing CI fuzz coverage now exercises both seeds.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff only touches comments/docstrings in wire/common.go and wire/msgtx.go, adds clarifying comments in wire/decode_budget_bench_test.go, and clones byte slices before f.Add in wire/fuzz_test.go. The fuzz fix ensures the coinbase transaction seed is not overwritten by the multi-input transaction seed because testing.F.Add retains a reference to the passed slice. No runtime decoding/encoding logic is altered.
Changed components
wire/common.go (comments only)wire/msgtx.go (comments only)wire/decode_budget_bench_test.go (benchmark comments only)wire/fuzz_test.go (fuzz seed handling)Inspect captured patch +29 / −48
### wire/common.go
@@ -770,11 +770,7 @@ func ReadVarString(r io.Reader, pver uint32) (string, error) {
// maximum block payload size since it helps protect against memory exhaustion
// attacks and forced panics through malformed messages.
//
-// If b is non-nil, the provided buffer will be used for serializing small
-// values. Otherwise a buffer will be drawn from the binarySerializer's pool
-// and return when the method finishes.
-//
-// NOTE: b MUST either be nil or at least an 8-byte slice.
+// NOTE: buf MUST be at least an 8-byte slice.
func readVarStringBuf(r io.Reader, pver uint32, buf []byte) (string, error) {
count, err := ReadVarIntBuf(r, pver, buf)
if err != nil {
@@ -812,11 +808,7 @@ func WriteVarString(w io.Writer, pver uint32, str string) error {
// the length of the string followed by the bytes that represent the string
// itself.
//
-// If b is non-nil, the provided buffer will be used for serializing small
-// values. Otherwise a buffer will be drawn from the binarySerializer's pool
-// and return when the method finishes.
-//
-// NOTE: b MUST either be nil or at least an 8-byte slice.
+// NOTE: buf MUST be at least an 8-byte slice.
func writeVarStringBuf(w io.Writer, pver uint32, str string, buf []byte) error {
err := WriteVarIntBuf(w, pver, uint64(len(str)), buf)
if err != nil {
@@ -850,9 +842,9 @@ func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
// passed maxAllowed parameter which helps protect against memory exhaustion
// attacks and forced panics through malformed messages. The fieldName
// parameter is only used for the error message so it provides more context in
-// the error. If b is non-nil, the provided buffer will be used for serializing
-// small values. Otherwise a buffer will be drawn from the binarySerializer's
-// pool and return when the method finishes.
+// the error.
+//
+// NOTE: buf MUST be at least an 8-byte slice.
func ReadVarBytesBuf(r io.Reader, pver uint32, buf []byte, maxAllowed uint32,
fieldName string) ([]byte, error) {
### wire/decode_budget_bench_test.go
@@ -130,6 +130,9 @@ func BenchmarkTruncatedDecodeBudget(b *testing.B) {
})
b.Run("merkle_hashes", func(b *testing.B) {
+ // The 80-byte block header followed by the 4-byte total transaction
+ // count are read by MsgMerkleBlock.BtcDecode before the hash count
+ // varint.
payload := benchmarkCountPayload(
b, make([]byte, MaxBlockHeaderPayload+4), maxTxPerBlock,
)
@@ -201,6 +204,8 @@ func BenchmarkTruncatedDecodeBudget(b *testing.B) {
})
b.Run("streaming_filter_checkpoints", func(b *testing.B) {
+ // The 1-byte filter type followed by the 32-byte stop hash are read
+ // by MsgCFCheckpt.BtcDecode before the filter header count varint.
payload := benchmarkCountPayload(
b, make([]byte, 33), maxCFHeadersLen,
)
### wire/fuzz_test.go
@@ -20,13 +20,17 @@ func FuzzTxDecode(f *testing.F) {
if err := blockOne.Transactions[0].Serialize(&seed); err != nil {
f.Fatal(err)
}
- f.Add(seed.Bytes())
+ f.Add(bytes.Clone(seed.Bytes()))
seed.Reset()
if err := multiTx.Serialize(&seed); err != nil {
f.Fatal(err)
}
- f.Add(seed.Bytes())
+ // The seed must be cloned rather than added directly: f.Add keeps a
+ // reference to the slice it is given, and reusing the buffer below
+ // would overwrite the coinbase seed before the corpus entry ever
+ // runs.
+ f.Add(bytes.Clone(seed.Bytes()))
f.Add(multiWitnessTxEncoded)
@@ -74,7 +78,7 @@ func FuzzBlockDecode(f *testing.F) {
if err := blockOne.Serialize(&seed); err != nil {
f.Fatal(err)
}
- f.Add(seed.Bytes())
+ f.Add(bytes.Clone(seed.Bytes()))
f.Fuzz(func(t *testing.T, data []byte) {
var block MsgBlock
### wire/msgtx.go
@@ -963,11 +963,7 @@ func NewMsgTx(version int32) *MsgTx {
// readOutPointBuf reads the next sequence of bytes from r as an OutPoint.
//
-// If b is non-nil, the provided buffer will be used for serializing small
-// values. Otherwise a buffer will be drawn from the binarySerializer's pool
-// and return when the method finishes.
-//
-// NOTE: b MUST either be nil or at least an 8-byte slice.
+// NOTE: buf MUST be at least an 8-byte slice.
func readOutPointBuf(r io.Reader, pver uint32, version int32, op *OutPoint,
buf []byte) error {
@@ -997,11 +993,7 @@ func WriteOutPoint(w io.Writer, pver uint32, version int32, op *OutPoint) error
// writeOutPointBuf encodes op to the bitcoin protocol encoding for an OutPoint
// to w.
//
-// If b is non-nil, the provided buffer will be used for serializing small
-// values. Otherwise a buffer will be drawn from the binarySerializer's pool
-// and return when the method finishes.
-//
-// NOTE: b MUST either be nil or at least an 8-byte slice.
+// NOTE: buf MUST be at least an 8-byte slice.
func writeOutPointBuf(w io.Writer, pver uint32, version int32, op *OutPoint,
buf []byte) error {
@@ -1025,11 +1017,7 @@ func writeOutPointBuf(w io.Writer, pver uint32, version int32, op *OutPoint,
// parameter is only used for the error message so it provides more context in
// the error.
//
-// If b is non-nil, the provided buffer will be used for serializing small
-// values. Otherwise a buffer will be drawn from the binarySerializer's pool
-// and return when the method finishes.
-//
-// NOTE: b MUST either be nil or at least an 8-byte slice.
+// NOTE: buf MUST be at least an 8-byte slice.
func readScriptBuf(r io.Reader, pver uint32, buf []byte, ar *scriptArena,
fieldName string) ([]byte, error) {
@@ -1069,11 +1057,7 @@ func readScriptBuf(r io.Reader, pver uint32, buf []byte, ar *scriptArena,
// readTxInBuf reads the next sequence of bytes from r as a transaction input
// (TxIn).
//
-// If b is non-nil, the provided buffer will be used for serializing small
-// values. Otherwise a buffer will be drawn from the binarySerializer's pool
-// and return when the method finishes.
-//
-// NOTE: b MUST either be nil or at least an 8-byte slice.
+// NOTE: buf MUST be at least an 8-byte slice.
func readTxInBuf(r io.Reader, pver uint32, version int32, ti *TxIn,
buf []byte, ar *scriptArena) error {
@@ -1099,9 +1083,8 @@ func readTxInBuf(r io.Reader, pver uint32, version int32, ti *TxIn,
}
// writeTxInBuf encodes ti to the bitcoin protocol encoding for a transaction
-// input (TxIn) to w. If b is non-nil, the provided buffer will be used for
-// serializing small values. Otherwise a buffer will be drawn from the
-// binarySerializer's pool and return when the method finishes.
+// input (TxIn) to w. The buf parameter is the scratch buffer used for
+// serializing small values.
func writeTxInBuf(w io.Writer, pver uint32, version int32, ti *TxIn,
buf []byte) error {
@@ -1146,9 +1129,8 @@ func ReadTxOut(r io.Reader, pver uint32, version int32, to *TxOut) error {
}
// readTxOutBuf reads the next sequence of bytes from r as a transaction output
-// (TxOut). If b is non-nil, the provided buffer will be used for serializing
-// small values. Otherwise a buffer will be drawn from the binarySerializer's
-// pool and return when the method finishes.
+// (TxOut). The buf parameter is the scratch buffer used for serializing
+// small values.
func readTxOutBuf(r io.Reader, pver uint32, version int32, to *TxOut,
buf []byte, ar *scriptArena) error {
@@ -1178,9 +1160,8 @@ func WriteTxOut(w io.Writer, pver uint32, version int32, to *TxOut) error {
}
// WriteTxOutBuf encodes to into the bitcoin protocol encoding for a transaction
-// output (TxOut) to w. If b is non-nil, the provided buffer will be used for
-// serializing small values. Otherwise a buffer will be drawn from the
-// binarySerializer's pool and return when the method finishes.
+// output (TxOut) to w. The buf parameter is the scratch buffer used for
+// serializing small values.
//
// NOTE: This function is exported in order to allow txscript to compute the
// new sighashes for witness transactions (BIP0143).
@@ -1197,9 +1178,8 @@ func WriteTxOutBuf(w io.Writer, pver uint32, version int32, to *TxOut,
}
// writeTxWitnessBuf encodes the bitcoin protocol encoding for a transaction
-// input's witness into to w. If b is non-nil, the provided buffer will be used
-// for serializing small values. Otherwise a buffer will be drawn from the
-// binarySerializer's pool and return when the method finishes.
+// input's witness into to w. The buf parameter is the scratch buffer used
+// for serializing small values.
func writeTxWitnessBuf(w io.Writer, pver uint32, version int32, wit [][]byte,
buf []byte) error {
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.