mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: prevent nil dereferences on cmd handlers (#8319)
* chore: detect nil cmd handlers Prevent nil panic dereferences on cmd handlers. Add a unit test to prevent future mistakes
This commit is contained in:
+12
@@ -260,6 +260,18 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) {
|
||||
// Add a wrapper to every command to enable debugging options.
|
||||
cmd.Walk(func(cmd *clibase.Cmd) {
|
||||
h := cmd.Handler
|
||||
if h == nil {
|
||||
// We should never have a nil handler, but if we do, do not
|
||||
// wrap it. Wrapping it just hides a nil pointer dereference.
|
||||
// If a nil handler exists, this is a developer bug. If no handler
|
||||
// is required for a command such as command grouping (e.g. `users'
|
||||
// and 'groups'), then the handler should be set to the helper
|
||||
// function.
|
||||
// func(inv *clibase.Invocation) error {
|
||||
// return inv.Command.HelpHandler(inv)
|
||||
// }
|
||||
return
|
||||
}
|
||||
cmd.Handler = func(i *clibase.Invocation) error {
|
||||
if !debugOptions {
|
||||
return h(i)
|
||||
|
||||
Reference in New Issue
Block a user