fix: fixes use of possibly nil RemoteAddr() and LocalAddr() return values (#21076)

fixes: https://github.com/coder/internal/issues/1143

Both gVisor and the Go standard library implementations of `net.Conn` can under certain circumstances return `nil` for `RemoteAddr()` and `LocalAddr()` calls. If we call their methods, we segfault.

This PR fixes these calls and adds ruleguard rules.

Note that `slog.F("remote_addr", conn.RemoteAddr())` is fine because slog detects the `nil` before attempting to stringify the type.
This commit is contained in:
Spike Curtis
2025-12-03 15:06:00 +04:00
committed by GitHub
parent 65ef6df1df
commit 40df21ed62
8 changed files with 94 additions and 9 deletions
+3 -1
View File
@@ -153,7 +153,9 @@ func freeUDPPort(t *testing.T) uint16 {
})
require.NoError(t, err, "listen on random UDP port")
_, port, err := net.SplitHostPort(l.LocalAddr().String())
localAddr := l.LocalAddr()
require.NotNil(t, localAddr, "local address is nil")
_, port, err := net.SplitHostPort(localAddr.String())
require.NoError(t, err, "split host port")
portUint, err := strconv.ParseUint(port, 10, 16)