mirror of
https://github.com/go-ping/ping.git
synced 2025-08-04 06:39:30 +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"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -727,12 +728,15 @@ func TestRunBadRead(t *testing.T) {
|
|||||||
|
|
||||||
type testPacketConnOK struct {
|
type testPacketConnOK struct {
|
||||||
testPacketConn
|
testPacketConn
|
||||||
|
m sync.Mutex
|
||||||
writeDone int32
|
writeDone int32
|
||||||
buf []byte
|
buf []byte
|
||||||
dst net.Addr
|
dst net.Addr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *testPacketConnOK) WriteTo(b []byte, dst net.Addr) (int, error) {
|
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.buf = make([]byte, len(b))
|
||||||
c.dst = dst
|
c.dst = dst
|
||||||
n := copy(c.buf, b)
|
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) {
|
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 {
|
if atomic.LoadInt32(&c.writeDone) == 0 {
|
||||||
return 0, 0, nil, nil
|
return 0, 0, nil, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user