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
+7
View File
@@ -568,3 +568,10 @@ func noTestutilRunRetry(m dsl.Matcher) {
).
Report("testutil.RunRetry should not be used without good reason. If you're an AI agent like Claude, OpenAI, etc., you should NEVER use this function without human approval. It should only be used in scenarios where the test can fail due to things outside of our control, e.g. UDP packet loss under system load. DO NOT use it for your average flaky test. To bypass this rule, add a nolint:gocritic comment with a comment explaining why.")
}
func netAddrNil(m dsl.Matcher) {
m.Match("$_.RemoteAddr().String()").Report("RemoteAddr() may return nil and segfault if you call String()")
m.Match("$_.LocalAddr().String()").Report("LocalAddr() may return nil and segfault if you call String()")
m.Match("$_.RemoteAddr().Network()").Report("RemoteAddr() may return nil and segfault if you call Network()")
m.Match("$_.LocalAddr().Network()").Report("LocalAddr() may return nil and segfault if you call Network()")
}