From ff8e5a3685cdebf52fe4667a24bb756c3a30b897 Mon Sep 17 00:00:00 2001 From: Jim Date: Mon, 12 Aug 2024 15:33:02 -0400 Subject: [PATCH] fix (oidc/examples/cli): return free port err properly --- oidc/examples/cli/main.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/oidc/examples/cli/main.go b/oidc/examples/cli/main.go index 0bba9be..bfb7395 100644 --- a/oidc/examples/cli/main.go +++ b/oidc/examples/cli/main.go @@ -111,7 +111,7 @@ func main() { } oidcPort := os.Getenv("OIDC_PORT") if oidcPort == "" { - oidcPort, err = func() (string, error) { + oidcPort, err = func() (port string, retErr error) { addr, err := net.ResolveTCPAddr("tcp", "localhost:0") if err != nil { return "", err @@ -121,13 +121,23 @@ func main() { if err != nil { return "", err } - defer l.Close() - return strconv.Itoa(l.Addr().(*net.TCPAddr).Port), nil + defer func() { + if l != nil { + if err := l.Close(); err != nil { + retErr = err + } + } + }() + tcpAddr, ok := l.Addr().(*net.TCPAddr) + if !ok { + return "", fmt.Errorf("not a net.TCPAddr") + } + return strconv.Itoa(tcpAddr.Port), nil }() if err != nil { fmt.Fprintf(os.Stderr, "env OIDC_PORT is empty and error finding a free port: %s", err.Error()) + return } - return } id, secret := "test-rp", "fido"