Skip to content

Commit

Permalink
Ensure infinite loop cannot occur in parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rliebz committed Oct 12, 2019
1 parent df740f6 commit f5306b6
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions parse.go
Expand Up @@ -28,6 +28,7 @@ func parseIter(set *flag.FlagSet, ip iterativeParser, args []string) error {
}

// regenerate the initial args with the split short opts
argsWereSplit := false
for i, arg := range args {
// skip args that are not part of the error message
if name := strings.TrimLeft(arg, "-"); name != trimmed {
Expand All @@ -42,9 +43,16 @@ func parseIter(set *flag.FlagSet, ip iterativeParser, args []string) error {

// swap current argument with the split version
args = append(args[:i], append(shortOpts, args[i+1:]...)...)
argsWereSplit = true
break
}

// This should be an impossible to reach code path, but in case the arg
// splitting failed to happen, this will prevent infinite loops
if !argsWereSplit {
return err
}

// Since custom parsing failed, replace the flag set before retrying
newSet, err := ip.newFlagSet()
if err != nil {
Expand Down

0 comments on commit f5306b6

Please sign in to comment.