Fix datarace in testPacketConnOK by adding a mutex when accessing testPacketConnOK's member variables

Signed-off-by: TheRushingWookie <3181551+TheRushingWookie@users.noreply.github.com>
This commit is contained in:
TheRushingWookie 2023-04-03 17:40:58 -05:00 committed by Kaj Niemi
parent 31b1b44067
commit a5e239147e

View File

@ -5,6 +5,7 @@ import (
"errors"
"net"
"runtime/debug"
"sync"
"sync/atomic"
"testing"
"time"
@ -727,12 +728,15 @@ func TestRunBadRead(t *testing.T) {
type testPacketConnOK struct {
testPacketConn
m sync.Mutex
writeDone int32
buf []byte
dst net.Addr
}
func (c *testPacketConnOK) WriteTo(b []byte, dst net.Addr) (int, error) {
c.m.Lock()
defer c.m.Unlock()
c.buf = make([]byte, len(b))
c.dst = dst
n := copy(c.buf, b)
@ -741,6 +745,8 @@ func (c *testPacketConnOK) WriteTo(b []byte, dst net.Addr) (int, error) {
}
func (c *testPacketConnOK) ReadFrom(b []byte) (n int, ttl int, src net.Addr, err error) {
c.m.Lock()
defer c.m.Unlock()
if atomic.LoadInt32(&c.writeDone) == 0 {
return 0, 0, nil, nil
}