Skip to content

chore: prevent nil dereferences on cmd handlers #8319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 5, 2023
Merged

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Jul 5, 2023

Prevent nil panic dereferences on cmd handlers. Add a unit test to prevent future mistakes

Fixes: #8315

Prevent nil panic dereferences on cmd handlers. Add a unit test
to prevent future mistakes
@Emyrk Emyrk changed the title chore: detect nil cmd handlers chore: prevent nil dereferences on cmd handlers Jul 5, 2023
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.
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This explains what, but not why? I'm guessing it's the call to h below, but why don't we just default to h being func(inv *clibase.Invocation) error { return inv.Command.HelpHandler(inv) } in this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mafredri I was wondering if defaulting to the help command would be a good solution.

Atm we manually invoke the help on all cmds except this one. So if I made that change it would be a new behavior that is probably fine, but being explicit is also pretty easy. Now that I have a unit test that will fail if you add a new cmd without a help, I figured I'd keep it being manual.

I'll update the comment with more explanation as to why. The unit test failure has that language near it

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your reasoning is sound! 😄

@Emyrk Emyrk enabled auto-merge (squash) July 5, 2023 17:12
@Emyrk Emyrk merged commit 4a008a8 into main Jul 5, 2023
@Emyrk Emyrk deleted the stevenmasley/cmd_help branch July 5, 2023 17:20
@github-actions github-actions bot locked and limited conversation to collaborators Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: nil pointer dereference in cli
2 participants