mirror of
https://github.com/go-ping/ping.git
synced 2025-08-01 21:49:06 +00:00
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:
parent
31b1b44067
commit
a5e239147e
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user