You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BoolWithInverseFlag.String() panics when FlagStringer returns string without tab #2303
BoolWithInverseFlag.String() panics with slice bounds out of range [-1:] when the FlagStringer function returns a string that does not contain a tab character. Since FlagStringer is a public variable that users are encouraged to override, this panic is reachable through documented API usage.
// flag_bool_with_inverse.go:170-183func (bif*BoolWithInverseFlag) String() string {
out:=FlagStringer(bif)
i:=strings.Index(out, "\t")
prefix:="--"// single character flags are prefixed with `-` instead of `--`iflen(bif.Name) ==1 {
prefix="-"
}
returnfmt.Sprintf("%s[%s]%s%s", prefix, bif.inversePrefix(), bif.Name, out[i:])
}
When FlagStringer returns a string without \t, strings.Index returns -1, and out[-1:] panics. The default stringifyFlag can also return "" (no tab) when the flag doesn't satisfy DocGenerationFlag.
Reproduction
package main
import (
"fmt""github.com/urfave/cli/v3"
)
funcmain() {
cli.FlagStringer=func(f cli.Flag) string {
return"custom output without tab"
}
flag:=&cli.BoolWithInverseFlag{
BoolFlag: cli.BoolFlag{
Name: "verbose",
},
}
fmt.Println(flag.String())
// panic: runtime error: slice bounds out of range [-1:]
}
Why This Matters
BoolWithInverseFlag.String() is an exported method implementing fmt.Stringer — called implicitly by fmt.Print, logging, error messages
FlagStringer is a documented public variable that users are encouraged to override for custom help output
The panic is undocumented and cannot be anticipated by users setting a custom FlagStringer
Note: FlagBase[T].String() in flag_impl.go:226 calls FlagStringer(f) directly without tab parsing, so it is not affected — only BoolWithInverseFlag has this issue
Activity
flag: prevent BoolWithInverseFlag.String from panicking without a tab
alliasgher commentedon Apr 13, 2026
Opened #2311 for this.
flag: prevent BoolWithInverseFlag.String from panicking without a tab