From 05410034e3e29662bb2058a584c001386d2ed284 Mon Sep 17 00:00:00 2001 From: JackDoan Date: Wed, 14 Oct 2026 15:30:51 -0500 Subject: [PATCH] handshake/fec: Encode a message from its own bytes, copying only a padded last symbol --- handshake/fec/fec.go | 10 ++++++---- handshake/fragment/sender.go | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/handshake/fec/fec.go b/handshake/fec/fec.go index 15122a83..adcec3af 100644 --- a/handshake/fec/fec.go +++ b/handshake/fec/fec.go @@ -58,18 +58,20 @@ type Encoder struct { data [][]byte } -// NewEncoder copies msg into padded data symbols. +// NewEncoder splits msg into data symbols. They alias msg, all but a padded +// last symbol, so msg must not change while the Encoder is in use. func NewEncoder(msg []byte, maxSymbolSize int) (*Encoder, error) { l, err := NewLayout(len(msg), maxSymbolSize) if err != nil { return nil, err } - buf := make([]byte, l.K*l.SymbolSize) - copy(buf, msg) data := make([][]byte, l.K) for i := range data { - data[i] = buf[i*l.SymbolSize : (i+1)*l.SymbolSize] + data[i] = msg[i*l.SymbolSize : min((i+1)*l.SymbolSize, len(msg))] + } + if last := data[l.K-1]; len(last) < l.SymbolSize { + data[l.K-1] = append(make([]byte, 0, l.SymbolSize), last...)[:l.SymbolSize] } return &Encoder{layout: l, data: data}, nil } diff --git a/handshake/fragment/sender.go b/handshake/fragment/sender.go index cfa5d230..3cea9218 100644 --- a/handshake/fragment/sender.go +++ b/handshake/fragment/sender.go @@ -34,7 +34,8 @@ type Sender struct { } // NewSender prepares msg for sending. subtype, remoteIndex and counter fill the -// Nebula header of every fragment; fragID is the sender's local index. +// Nebula header of every fragment; fragID is the sender's local index. The +// Sender keeps msg, which must not change while it is in use. func NewSender(msg []byte, subtype header.MessageSubType, remoteIndex uint32, counter uint64, fragID uint32) (*Sender, error) { if len(msg) == 0 || len(msg) > math.MaxUint16 { return nil, ErrMsgLen