1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:44:41 -07:00

net: document that user shouldn't modify returned Addr

Ideally, those methods should return a copy of the Addr, but
due to the Go 1 API guarantee, we cannot make that change now:
there might exist client code that uses the returned Addr as
map index and thus relies on the fact that different invocation
of the method returns the same pointer. Changing this behavior
will lead to hidden behaviour change in those programs.

Update #9654.

Change-Id: Iad4235f2ed7789b3a3c8e0993b9718cf0534ea2b
Reviewed-on: https://go-review.googlesource.com/3851
Reviewed-by: Mikio Hara <mikioh.mikioh@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Shenghou Ma 2015-02-03 12:59:40 -05:00 committed by Minux Ma
parent 4ce06f4b5c
commit 7e43aee301
5 changed files with 12 additions and 0 deletions

View File

@ -135,6 +135,8 @@ func (c *conn) Close() error {
} }
// LocalAddr returns the local network address. // LocalAddr returns the local network address.
// The Addr returned is shared by all invocations of LocalAddr, so
// do not modify it.
func (c *conn) LocalAddr() Addr { func (c *conn) LocalAddr() Addr {
if !c.ok() { if !c.ok() {
return nil return nil
@ -143,6 +145,8 @@ func (c *conn) LocalAddr() Addr {
} }
// RemoteAddr returns the remote network address. // RemoteAddr returns the remote network address.
// The Addr returned is shared by all invocations of RemoteAddr, so
// do not modify it.
func (c *conn) RemoteAddr() Addr { func (c *conn) RemoteAddr() Addr {
if !c.ok() { if !c.ok() {
return nil return nil

View File

@ -157,6 +157,8 @@ func (l *TCPListener) Close() error {
} }
// Addr returns the listener's network address, a *TCPAddr. // Addr returns the listener's network address, a *TCPAddr.
// The Addr returned is shared by all invocations of Addr, so
// do not modify it.
func (l *TCPListener) Addr() Addr { return l.fd.laddr } func (l *TCPListener) Addr() Addr { return l.fd.laddr }
// SetDeadline sets the deadline associated with the listener. // SetDeadline sets the deadline associated with the listener.

View File

@ -258,6 +258,8 @@ func (l *TCPListener) Close() error {
} }
// Addr returns the listener's network address, a *TCPAddr. // Addr returns the listener's network address, a *TCPAddr.
// The Addr returned is shared by all invocations of Addr, so
// do not modify it.
func (l *TCPListener) Addr() Addr { return l.fd.laddr } func (l *TCPListener) Addr() Addr { return l.fd.laddr }
// SetDeadline sets the deadline associated with the listener. // SetDeadline sets the deadline associated with the listener.

View File

@ -115,6 +115,8 @@ func (l *UnixListener) Close() error {
} }
// Addr returns the listener's network address. // Addr returns the listener's network address.
// The Addr returned is shared by all invocations of Addr, so
// do not modify it.
func (l *UnixListener) Addr() Addr { return nil } func (l *UnixListener) Addr() Addr { return nil }
// SetDeadline sets the deadline associated with the listener. // SetDeadline sets the deadline associated with the listener.

View File

@ -321,6 +321,8 @@ func (l *UnixListener) Close() error {
} }
// Addr returns the listener's network address. // Addr returns the listener's network address.
// The Addr returned is shared by all invocations of Addr, so
// do not modify it.
func (l *UnixListener) Addr() Addr { return l.fd.laddr } func (l *UnixListener) Addr() Addr { return l.fd.laddr }
// SetDeadline sets the deadline associated with the listener. // SetDeadline sets the deadline associated with the listener.