Ensure infinite loop cannot occur in parsing

This commit is contained in:
Robert Liebowitz 2019-10-11 22:57:55 -04:00
parent df740f6562
commit f5306b622f

View File

@ -28,6 +28,7 @@ func parseIter(set *flag.FlagSet, ip iterativeParser, args []string) error {
} }
// regenerate the initial args with the split short opts // regenerate the initial args with the split short opts
argsWereSplit := false
for i, arg := range args { for i, arg := range args {
// skip args that are not part of the error message // skip args that are not part of the error message
if name := strings.TrimLeft(arg, "-"); name != trimmed { if name := strings.TrimLeft(arg, "-"); name != trimmed {
@ -42,9 +43,16 @@ func parseIter(set *flag.FlagSet, ip iterativeParser, args []string) error {
// swap current argument with the split version // swap current argument with the split version
args = append(args[:i], append(shortOpts, args[i+1:]...)...) args = append(args[:i], append(shortOpts, args[i+1:]...)...)
argsWereSplit = true
break 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 // Since custom parsing failed, replace the flag set before retrying
newSet, err := ip.newFlagSet() newSet, err := ip.newFlagSet()
if err != nil { if err != nil {