Skip to content

Commit 212aeff

Browse files
authored
fix(cli): fix potential panic in traceError if unwrapped err is nil (#15166)
Seen while investigating #12721: Root cause was a developer error, but this definitely shouldn't panic. Before: ``` / # coder stat panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x1f12eb0] goroutine 1 [running]: github.com/coder/coder/v2/cli.traceError({0x90e89a0?, 0x40007a8210}) /home/runner/work/coder/coder/cli/root.go:1119 +0x70 github.com/coder/coder/v2/cli.cliHumanFormatError({0x40003065a0, 0x1c8}, {0x90e89a0, 0x40007a8210}, 0x40007a81e0?) /home/runner/work/coder/coder/cli/root.go:985 +0x190 github.com/coder/coder/v2/cli.cliHumanFormatError({0x40000d0f00, 0x139}, {0x90e89a0, 0x40007a81e0}, 0x40001c4480?) /home/runner/work/coder/coder/cli/root.go:985 +0x1d8 github.com/coder/coder/v2/cli.cliHumanFormatError({0x40000d0b40, 0xf}, {0x90e5f00, 0x40006a3a80}, 0x90e5d40?) /home/runner/work/coder/coder/cli/root.go:985 +0x1d8 github.com/coder/coder/v2/cli.cliHumanFormatError({0x0, 0x0}, {0x90e5ce0, 0x40003b14c0}, 0x2?) /home/runner/work/coder/coder/cli/root.go:985 +0x1d8 github.com/coder/coder/v2/cli.formatRunCommandError(0x40007a8108, 0x400079fce7) /home/runner/work/coder/coder/cli/root.go:1057 +0x30c github.com/coder/coder/v2/cli.cliHumanFormatError({0x0, 0x0}, {0x90e5ec0, 0x40007a8108}, 0xaa0aed0?) /home/runner/work/coder/coder/cli/root.go:980 +0xe0 github.com/coder/coder/v2/cli.cliHumanFormatError({0x0, 0x0}, {0x90e5160, 0x40007a8120}, 0x90e50e0?) /home/runner/work/coder/coder/cli/root.go:966 +0x144 github.com/coder/coder/v2/cli.(*PrettyErrorFormatter).Format(0x400079fda0, {0x90e5160?, 0x40007a8120?}) /home/runner/work/coder/coder/cli/root.go:927 +0x48 github.com/coder/coder/v2/cli.(*RootCmd).RunWithSubcommands(0x400068ed80, {0x400053a2c8, 0x30, 0x57}) /home/runner/work/coder/coder/cli/root.go:175 +0x278 main.main() /home/runner/work/coder/coder/enterprise/cmd/coder/main.go:11 +0x40 ``` After: ``` Encountered an error running "coder stat", see "coder stat --help" for more information error: <nil> ```
1 parent 23f61c6 commit 212aeff

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

cli/root.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,16 @@ func formatCoderSDKError(from string, err *codersdk.Error, opts *formatOpts) str
11161116
//nolint:errorlint
11171117
func traceError(err error) string {
11181118
if uw, ok := err.(interface{ Unwrap() error }); ok {
1119-
a, b := err.Error(), uw.Unwrap().Error()
1119+
var a, b string
1120+
if err != nil {
1121+
a = err.Error()
1122+
}
1123+
if uw != nil {
1124+
uwerr := uw.Unwrap()
1125+
if uwerr != nil {
1126+
b = uwerr.Error()
1127+
}
1128+
}
11201129
c := strings.TrimSuffix(a, b)
11211130
return c
11221131
}

0 commit comments

Comments
 (0)