Skip to content

flag: prevent BoolWithInverseFlag.String from panicking without a tab#2311

Open
alliasgher wants to merge 1 commit intourfave:mainfrom
alliasgher:fix-boolwithinverse-string-panic
Open

flag: prevent BoolWithInverseFlag.String from panicking without a tab#2311
alliasgher wants to merge 1 commit intourfave:mainfrom
alliasgher:fix-boolwithinverse-string-panic

Conversation

@alliasgher
Copy link
Copy Markdown

Summary

BoolWithInverseFlag.String() panics with slice bounds out of range [-1:] when FlagStringer returns a string without a tab character.

The code uses strings.Index(out, "\t") and then slices out[i:]. When no tab is present, i is -1 and out[-1:] panics.

Before

// FlagStringer returns a string with no \t (custom stringer, or default
// when flag doesn't implement DocGenerationFlag)
cli.FlagStringer = func(f cli.Flag) string { return "no tab" }
flag.String() // ← panic: slice bounds out of range [-1:]

Fix

Clamp i to 0 when strings.Index returns -1. In that case the entire stringer output is used as the suffix, which is the safest non-panicking behaviour.

Fixes #2303

Checklist

  • Tests added (TestBoolWithInverseFlagStringNoPanicWithNoTabStringer)
  • Full test suite passes

When FlagStringer returns a string without a tab character,
strings.Index returns -1 and out[-1:] panics with
"slice bounds out of range [-1:]".

Guard against this by clamping the index to 0 when no tab is
found. In that case the entire FlagStringer output is used as
the suffix portion of the formatted string, matching the intent
of callers who use a custom stringer without the usual tab layout.

Fixes urfave#2303

Signed-off-by: alliasgher <alliasgher123@gmail.com>
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.

BoolWithInverseFlag.String() panics when FlagStringer returns string without tab

1 participant