Skip to content

Commit

Permalink
Make Packet.MarshalTo() thread-safe
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aler9 committed Mar 17, 2022
1 parent beba640 commit 7772c8f
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 1 deletion.
1 change: 0 additions & 1 deletion packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func TestBasic(t *testing.T) {
}},
},
Version: 2,
Padding: true,
PayloadType: 96,
SequenceNumber: 27023,
Timestamp: 3653407706,
Expand Down

0 comments on commit 7772c8f

Please sign in to comment.