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

Nil pointer when getting context's flagSet in UsageError function. #1325

Closed
5 of 7 tasks
WUST-mengqinyu opened this issue Jan 11, 2022 · 0 comments · Fixed by #1327
Closed
5 of 7 tasks

Nil pointer when getting context's flagSet in UsageError function. #1325

WUST-mengqinyu opened this issue Jan 11, 2022 · 0 comments · Fixed by #1327
Labels
area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this

Comments

@WUST-mengqinyu
Copy link
Contributor

my urfave/cli version is

v2.3.0

Checklist

  • Are you running the latest v2 release? The list of releases is here.
  • Did you check the manual for your release? The v2 manual is here
  • Did you perform a search about this problem? Here's the Github guide about searching.

Dependency Management

  • My project is using go modules.
  • My project is using vendoring.
  • My project is automatically downloading the latest version.
  • I am unsure of what my dependency management setup is.

Describe the bug

When I try to get flag from UsageError function, program panic because cli.Context's flagSet is a nil pointer.

To reproduce

package main

import (
	"fmt"

	"github.com/urfave/cli/v2"
)

func main() {
	app := cli.NewApp()
	app.Commands = []*cli.Command{
		{
			Name: "test",
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name: "flag",
				},
			},
			OnUsageError: UsageError,
		},
	}
	err := app.Run([]string{"@app", "test", "--flag", "1", "--notflag", "1"})
	fmt.Println(err)
}

func UsageError(ctx *cli.Context, err error, isSubcommand bool) error {
	flag := ctx.String("flag")
	fmt.Println(flag)
	return nil
}

Observed behavior

image

panic: runtime error: invalid memory address or nil pointer dereference

[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x114b8aa]

Expected behavior

I expect cli.Context can Lineage correctly and find flag I need.

Additional context

Want to fix this yourself?

We'd love to have more contributors on this project! If the fix for this bug is easily explained and very small, free free to create a pull request for it.

Run go version and paste its output here

go version go1.16 darwin/amd64

Run go env and paste its output here

# paste `go env` output in here
@WUST-mengqinyu WUST-mengqinyu added area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this labels Jan 11, 2022
@WUST-mengqinyu WUST-mengqinyu changed the title Empty pointer when getting context's flagSet in UsageError function. Nil pointer when getting context's flagSet in UsageError function. Jan 11, 2022
rliebz pushed a commit that referenced this issue Jan 17, 2022
* fix: Nil pointer when getting context's flagSet in UsageError function(#1325)

* test: add unit test for nil flagset in Context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v2 relates to / is being considered for v2 kind/bug describes or fixes a bug status/triage maintainers still need to look into this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant