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{} { ...@@ -58,14 +58,15 @@ var pktBufPool = sync.Pool{New: func() interface{} {
}} }}
// pktAlloc allocates pktBuf with len=n. // pktAlloc allocates pktBuf with len=n.
//
// n must be >= sizeof(proto.PktHeader).
func pktAlloc(n int) *pktBuf { func pktAlloc(n int) *pktBuf {
if n < proto.PktHeaderLen { // make sure cap >= PktHeaderLen.
panic("pktAlloc: n < sizeof(PktHeader)") // see Header for why
l := n
if l < proto.PktHeaderLen {
l = proto.PktHeaderLen
} }
pkt := pktBufPool.Get().(*pktBuf) pkt := pktBufPool.Get().(*pktBuf)
pkt.data = xbytes.Realloc(pkt.data, n) pkt.data = xbytes.Realloc(pkt.data, l)[:n]
return pkt 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