Commit 2e5989a2 authored by Kirill Smelkov's avatar Kirill Smelkov

go/neo/neonet: Adjust pktAlloc to always allocate pktBuf with cap >= PktHeaderSize

With upcoming msgpack encoding packets might be with data < PktHeaderSize.
Adjust pktAlloc to unconditionally provide the invariant.
parent ceb8984b
......@@ -58,14 +58,15 @@ var pktBufPool = sync.Pool{New: func() interface{} {
}}
// pktAlloc allocates pktBuf with len=n.
//
// n must be >= sizeof(proto.PktHeader).
func pktAlloc(n int) *pktBuf {
if n < proto.PktHeaderLen {
panic("pktAlloc: n < sizeof(PktHeader)")
// make sure cap >= PktHeaderLen.
// see Header for why
l := n
if l < proto.PktHeaderLen {
l = proto.PktHeaderLen
}
pkt := pktBufPool.Get().(*pktBuf)
pkt.data = xbytes.Realloc(pkt.data, n)
pkt.data = xbytes.Realloc(pkt.data, l)[:n]
return pkt
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment