1
0
mirror of https://github.com/golang/go synced 2024-11-16 21:54:51 -07:00

net: return nil UDPAddr from ReadFromUDP

In cases where the socket operation has no underlying address,
golang.org/cl/291509 unintentionally changed ReadFromUDP from return a
nil *UDPAddr to a non-nil (but zero value) *UDPAddr.

This may break callers that assume "no address" is always addr == nil,
so change it back to remain nil.

Fixes #46238

Change-Id: I8531e8fa16b853ed7560088eabda0b9e3e53f5be
Reviewed-on: https://go-review.googlesource.com/c/go/+/320909
Trust: Michael Pratt <mpratt@google.com>
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
Michael Pratt 2021-05-18 12:23:56 -04:00
parent 15a374d5c1
commit 658b5e66ec
2 changed files with 32 additions and 0 deletions

View File

@ -50,6 +50,9 @@ func (c *UDPConn) readFrom(b []byte, addr *UDPAddr) (int, *UDPAddr, error) {
*addr = UDPAddr{IP: sa.Addr[0:], Port: sa.Port}
case *syscall.SockaddrInet6:
*addr = UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneCache.name(int(sa.ZoneId))}
default:
// No sockaddr, so don't return UDPAddr.
addr = nil
}
return n, addr, err
}

View File

@ -8,7 +8,9 @@
package net
import (
"errors"
"internal/testenv"
"os"
"reflect"
"runtime"
"testing"
@ -446,6 +448,33 @@ func TestUDPReadSizeError(t *testing.T) {
}
}
// TestUDPReadTimeout verifies that ReadFromUDP with timeout returns an error
// without data or an address.
func TestUDPReadTimeout(t *testing.T) {
la, err := ResolveUDPAddr("udp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
c, err := ListenUDP("udp4", la)
if err != nil {
t.Fatal(err)
}
defer c.Close()
c.SetDeadline(time.Now())
b := make([]byte, 1)
n, addr, err := c.ReadFromUDP(b)
if !errors.Is(err, os.ErrDeadlineExceeded) {
t.Errorf("ReadFromUDP got err %v want os.ErrDeadlineExceeded", err)
}
if n != 0 {
t.Errorf("ReadFromUDP got n %d want 0", n)
}
if addr != nil {
t.Errorf("ReadFromUDP got addr %+#v want nil", addr)
}
}
func BenchmarkWriteToReadFromUDP(b *testing.B) {
conn, err := ListenUDP("udp4", &UDPAddr{IP: IPv4(127, 0, 0, 1)})
if err != nil {