diff options
author | David Fifield <dcf@torproject.org> | 2023-03-13 19:36:09 +0000 |
---|---|---|
committer | David Fifield <dcf@torproject.org> | 2023-03-13 19:36:09 +0000 |
commit | ef51f2063e8747f4256e756a3f572875700b0190 (patch) | |
tree | 75b04949f1e895a215d19ef573803cc790be762c | |
parent | b63d2272bfd262f5166c44f8f88330ed7a732009 (diff) | |
parent | d2858aeb7ec50ae09b9a7e2e2a910ae31cec62bd (diff) | |
download | snowflake-ef51f2063e8747f4256e756a3f572875700b0190.tar.gz snowflake-ef51f2063e8747f4256e756a3f572875700b0190.zip |
Merge branch '40260-revert-queuepacketconn-ownership' into 'main'
Revert "Take ownership of buffer in QueuePacketConn QueueIncoming/WriteTo"
See merge request tpo/anti-censorship/pluggable-transports/snowflake!140
-rw-r--r-- | common/turbotunnel/queuepacketconn.go | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/common/turbotunnel/queuepacketconn.go b/common/turbotunnel/queuepacketconn.go index e11f8d8..14a9833 100644 --- a/common/turbotunnel/queuepacketconn.go +++ b/common/turbotunnel/queuepacketconn.go @@ -42,9 +42,8 @@ func NewQueuePacketConn(localAddr net.Addr, timeout time.Duration) *QueuePacketC } } -// QueueIncoming queues an incoming packet and its source address, to be -// returned in a future call to ReadFrom. This function takes ownership of p and -// the caller must not reuse it. +// QueueIncoming queues and incoming packet and its source address, to be +// returned in a future call to ReadFrom. func (c *QueuePacketConn) QueueIncoming(p []byte, addr net.Addr) { select { case <-c.closed: @@ -52,8 +51,11 @@ func (c *QueuePacketConn) QueueIncoming(p []byte, addr net.Addr) { return default: } + // Copy the slice so that the caller may reuse it. + buf := make([]byte, len(p)) + copy(buf, p) select { - case c.recvQueue <- taggedPacket{p, addr}: + case c.recvQueue <- taggedPacket{buf, addr}: default: // Drop the incoming packet if the receive queue is full. } @@ -82,20 +84,22 @@ func (c *QueuePacketConn) ReadFrom(p []byte) (int, net.Addr, error) { } // WriteTo queues an outgoing packet for the given address. The queue can later -// be retrieved using the OutgoingQueue method. This function takes ownership of -// p and the caller must not reuse it. +// be retrieved using the OutgoingQueue method. func (c *QueuePacketConn) WriteTo(p []byte, addr net.Addr) (int, error) { select { case <-c.closed: return 0, &net.OpError{Op: "write", Net: c.LocalAddr().Network(), Addr: c.LocalAddr(), Err: c.err.Load().(error)} default: } + // Copy the slice so that the caller may reuse it. + buf := make([]byte, len(p)) + copy(buf, p) select { - case c.clients.SendQueue(addr) <- p: - return len(p), nil + case c.clients.SendQueue(addr) <- buf: + return len(buf), nil default: // Drop the outgoing packet if the send queue is full. - return len(p), nil + return len(buf), nil } } |