From 7772c8fef3483e1ffec39f3745cc868feb763796 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Tue, 8 Mar 2022 16:23:56 +0100 Subject: [PATCH] Make Packet.MarshalTo() thread-safe A Marshal function shouldn't change any property of the object that is being marshaled. Otherwise, Marshal() is non-thread safe and can't be called by multiple goroutines in parallel. PR #155 makes Packet.Marshal() set the Packet.Header.Padding bit when Packet.PaddingSize is non-zero; while this is preferable from the user point of view, it doesn't allow thread safety. This patch fixes the issue. --- packet.go | 1 - packet_test.go | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/packet.go b/packet.go index b3ae124..383a2bb 100644 --- a/packet.go +++ b/packet.go @@ -470,7 +470,6 @@ func (p Packet) Marshal() (buf []byte, err error) { // MarshalTo serializes the packet and writes to the buffer. func (p Packet) MarshalTo(buf []byte) (n int, err error) { - p.Header.Padding = p.PaddingSize != 0 n, err = p.Header.MarshalTo(buf) if err != nil { return 0, err diff --git a/packet_test.go b/packet_test.go index 5d19c11..6067c28 100644 --- a/packet_test.go +++ b/packet_test.go @@ -251,6 +251,7 @@ func TestBasic(t *testing.T) { }}, }, Version: 2, + Padding: true, PayloadType: 96, SequenceNumber: 27023, Timestamp: 3653407706,