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

Fix infinite loop when parsing invalid flags for apps with short option handling #911

Merged
merged 2 commits into from
Oct 12, 2019

Conversation

rliebz
Copy link
Member

@rliebz rliebz commented Oct 12, 2019

The old implementation of iterative parsing assumed that the error messages returned from the flag library would come in like this:

$ my-app -invalid
flag provided but not defined: -invalid
$ my-app --invalid
flag provided but not defined: --invalid

It turns out the error message for both is flag provided but not defined: -invalid.

This faulty assumption meant the iterative flag-parsing logic used for short-option handling could get into an infinite loop, where the same arguments would be parsed each loop, producing the same error each time with no exit condition.

To reproduce:

package main

import (
	"log"
	"os"

	"github.com/urfave/cli"
)

func main() {
	app := cli.NewApp()
	app.UseShortOptionHandling = true
	log.Fatal(app.Run(os.Args))
}

The following will hang forever, as will any flag prefixed by --:

$ go run main.go --invalid

The new test cases now cover that scenario.

I also cleaned up the arg rebuilding logic a bit to avoid unnecessary appends and loop iterations, and I added a condition to guarantee that an infinite loop cannot occur for any reason in the future, even though that code path should theoretically be unreachable as it stands.

@rliebz rliebz requested a review from a team as a code owner October 12, 2019 03:04
@codecov
Copy link

codecov bot commented Oct 12, 2019

Codecov Report

Merging #911 into master will decrease coverage by 0.07%.
The diff coverage is 77.77%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #911      +/-   ##
==========================================
- Coverage   71.45%   71.38%   -0.08%     
==========================================
  Files          30       30              
  Lines        2393     2394       +1     
==========================================
- Hits         1710     1709       -1     
- Misses        577      578       +1     
- Partials      106      107       +1
Impacted Files Coverage Δ
parse.go 89.74% <77.77%> (-5%) ⬇️

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 8dcb75d...f5306b6. Read the comment docs.

Copy link
Member

@coilysiren coilysiren left a comment

Choose a reason for hiding this comment

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

✨ thanks! ✨

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, thanks!

@AudriusButkevicius AudriusButkevicius merged commit a221e66 into urfave:master Oct 12, 2019
@rliebz rliebz deleted the fix/infinite-parse branch October 12, 2019 16:55
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.

4 participants