Skip to content
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

Avoid panic for missing flag value #893

Merged
merged 1 commit into from Sep 14, 2019
Merged

Conversation

rliebz
Copy link
Member

@rliebz rliebz commented Sep 13, 2019

Currently, in cases where a flag value is required but not passed, and short option handling is enabled, a panic will occur due to a nil pointer dereference. This prevents that situation from occurring, instead propagating the appropriate flag-parsing error.

Small example to reproduce issue:

package main

import (
	"os"

	"github.com/urfave/cli"
)

func main() {
	app := cli.NewApp()
	app.UseShortOptionHandling = true
	app.Flags = []cli.Flag{
		cli.StringFlag{Name: "flag"},
	}
	app.Run(os.Args)
}

Before:

$ ./simple-app --flag
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x10d5d42]
...

After:

$ ./simple-app --flag
Incorrect Usage. flag needs an argument: -flag

NAME:
   simple-app - A new cli application

USAGE:
   simple-app [global options] command [command options] [arguments...]

VERSION:
   0.0.0

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --flag value
   --help, -h     show help
   --version, -v  print the version

@rliebz rliebz requested a review from a team as a code owner September 13, 2019 09:48
@codecov
Copy link

codecov bot commented Sep 13, 2019

Codecov Report

Merging #893 into master will increase coverage by 0.04%.
The diff coverage is 76.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #893      +/-   ##
==========================================
+ Coverage   71.41%   71.45%   +0.04%     
==========================================
  Files          30       30              
  Lines        2389     2393       +4     
==========================================
+ Hits         1706     1710       +4     
  Misses        577      577              
  Partials      106      106
Impacted Files Coverage Δ
app.go 84.45% <100%> (ø) ⬆️
command.go 82.58% <50%> (-0.98%) ⬇️
parse.go 94.73% <77.77%> (+5.54%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c71fbce...06e3d38. Read the comment docs.

Currently, in cases where a flag value is required but not passed and
short-option handling is enabled, a panic will occur due to a nil
pointer dereference. This prevents that situation from occurring,
instead propagating the appropriate error.
Copy link
Member

@saschagrunert saschagrunert left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants