refactor header writing to append to a byte slice (#3646)

This avoids having to allocate a bytes.Buffer.
This commit is contained in:
Marten Seemann 2023-01-17 01:56:06 -08:00 committed by GitHub
parent 3d4bbc28ba
commit c24fbb094c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 282 additions and 279 deletions

View file

@ -1,7 +1,6 @@
package main
import (
"bytes"
"log"
"math/rand"
@ -85,28 +84,28 @@ func main() {
PacketNumberLen: protocol.PacketNumberLen(rand.Intn(4) + 1),
PacketNumber: protocol.PacketNumber(rand.Uint64()),
}
b := &bytes.Buffer{}
if err := extHdr.Write(b, version); err != nil {
b, err := extHdr.Append(nil, version)
if err != nil {
log.Fatal(err)
}
if h.Type == protocol.PacketTypeRetry {
b.Write([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
b = append(b, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}...)
}
if h.Length > 0 {
b.Write(make([]byte, h.Length))
b = append(b, make([]byte, h.Length)...)
}
if err := helper.WriteCorpusFileWithPrefix("corpus", b.Bytes(), header.PrefixLen); err != nil {
if err := helper.WriteCorpusFileWithPrefix("corpus", b, header.PrefixLen); err != nil {
log.Fatal(err)
}
}
// short header
b := &bytes.Buffer{}
if err := wire.WriteShortHeader(b, protocol.ParseConnectionID(getRandomData(8)), 1337, protocol.PacketNumberLen2, protocol.KeyPhaseOne); err != nil {
b, err := wire.AppendShortHeader(nil, protocol.ParseConnectionID(getRandomData(8)), 1337, protocol.PacketNumberLen2, protocol.KeyPhaseOne)
if err != nil {
log.Fatal(err)
}
if err := helper.WriteCorpusFileWithPrefix("corpus", b.Bytes(), header.PrefixLen); err != nil {
if err := helper.WriteCorpusFileWithPrefix("corpus", b, header.PrefixLen); err != nil {
log.Fatal(err)
}

View file

@ -64,8 +64,8 @@ func Fuzz(data []byte) int {
if hdr.Length > 16383 {
return 1
}
b := &bytes.Buffer{}
if err := extHdr.Write(b, version); err != nil {
b, err := extHdr.Append(nil, version)
if err != nil {
// We are able to parse packets with connection IDs longer than 20 bytes,
// but in QUIC version 1, we don't write headers with longer connection IDs.
if hdr.DestConnectionID.Len() <= protocol.MaxConnIDLen &&
@ -76,8 +76,8 @@ func Fuzz(data []byte) int {
}
// GetLength is not implemented for Retry packets
if hdr.Type != protocol.PacketTypeRetry {
if expLen := extHdr.GetLength(version); expLen != protocol.ByteCount(b.Len()) {
panic(fmt.Sprintf("inconsistent header length: %#v. Expected %d, got %d", extHdr, expLen, b.Len()))
if expLen := extHdr.GetLength(version); expLen != protocol.ByteCount(len(b)) {
panic(fmt.Sprintf("inconsistent header length: %#v. Expected %d, got %d", extHdr, expLen, len(b)))
}
}
return 1