From 760eb190baf688d40d128b7c0a4b2d11139ebed0 Mon Sep 17 00:00:00 2001 From: alliasgher Date: Tue, 14 Apr 2026 03:39:33 +0500 Subject: [PATCH 1/2] flag: prevent BoolWithInverseFlag.String from panicking without a tab 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 #2303 Signed-off-by: alliasgher --- flag_bool_with_inverse.go | 8 ++++++++ flag_bool_with_inverse_test.go | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/flag_bool_with_inverse.go b/flag_bool_with_inverse.go index 272dd98fec..680fe2c64e 100644 --- a/flag_bool_with_inverse.go +++ b/flag_bool_with_inverse.go @@ -179,6 +179,14 @@ func (bif *BoolWithInverseFlag) String() string { prefix = "-" } + // Guard against a FlagStringer that returns a string without a tab (e.g. + // a custom stringer or the default stringer when the flag does not + // implement DocGenerationFlag). In that case treat the entire output as + // the tab-delimited suffix so the slice never goes out of bounds. + if i < 0 { + i = 0 + } + return fmt.Sprintf("%s[%s]%s%s", prefix, bif.inversePrefix(), bif.Name, out[i:]) } diff --git a/flag_bool_with_inverse_test.go b/flag_bool_with_inverse_test.go index 981494586b..fbb7e43ff8 100644 --- a/flag_bool_with_inverse_test.go +++ b/flag_bool_with_inverse_test.go @@ -520,3 +520,26 @@ func TestBoolWithInverseFlag_SatisfiesVisibleFlagInterface(t *testing.T) { _ = f.IsVisible() } + +// TestBoolWithInverseFlagStringNoPanicWithNoTabStringer is a regression test for +// https://github.com/urfave/cli/issues/2303. +// BoolWithInverseFlag.String() panicked with "slice bounds out of range [-1:]" +// when the FlagStringer returned a string without a tab character. +func TestBoolWithInverseFlagStringNoPanicWithNoTabStringer(t *testing.T) { + orig := FlagStringer + defer func() { FlagStringer = orig }() + + FlagStringer = func(f Flag) string { + return "no tab here" + } + + flag := &BoolWithInverseFlag{ + Name: "verbose", + } + + // Must not panic. + got := flag.String() + if !strings.Contains(got, "verbose") { + t.Errorf("expected String() to contain the flag name, got %q", got) + } +} From bb2e3ea0f522541c714f0b6f5e342e6dd9dd596e Mon Sep 17 00:00:00 2001 From: alliasgher Date: Tue, 14 Apr 2026 03:41:30 +0500 Subject: [PATCH 2/2] errors: don't print an empty line for Exit with empty message When HandleExitCoder received an ExitCoder whose Error() returned an empty string (e.g. cli.Exit("", code)), it still called fmt.Fprintln(ErrWriter, err) which wrote a bare newline to stderr. Guard the print with a check on the error message so that an empty message produces no output at all. Fixes #2263 Signed-off-by: alliasgher --- errors.go | 10 ++++++---- errors_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/errors.go b/errors.go index f365a57990..ffd6471f23 100644 --- a/errors.go +++ b/errors.go @@ -150,10 +150,12 @@ func HandleExitCoder(err error) { } if exitErr, ok := err.(ExitCoder); ok { - if _, ok := exitErr.(ErrorFormatter); ok { - _, _ = fmt.Fprintf(ErrWriter, "%+v\n", err) - } else { - _, _ = fmt.Fprintln(ErrWriter, err) + if msg := err.Error(); msg != "" { + if _, ok := exitErr.(ErrorFormatter); ok { + _, _ = fmt.Fprintf(ErrWriter, "%+v\n", err) + } else { + _, _ = fmt.Fprintln(ErrWriter, err) + } } OsExiter(exitErr.ExitCode()) return diff --git a/errors_test.go b/errors_test.go index 35aaab54d4..6a497c58f5 100644 --- a/errors_test.go +++ b/errors_test.go @@ -232,3 +232,28 @@ func TestErrRequiredFlags_Error(t *testing.T) { expectedMsg = "Required flag \"flag1\" not set" assert.Equal(t, expectedMsg, err.Error()) } + +// TestHandleExitCoder_EmptyMessage is a regression test for +// https://github.com/urfave/cli/issues/2263. +// HandleExitCoder must not print an empty line to ErrWriter when the ExitCoder +// message is empty (e.g. cli.Exit("", code)). +func TestHandleExitCoder_EmptyMessage(t *testing.T) { + called := false + + OsExiter = func(rc int) { + if !called { + called = true + } + } + ErrWriter = &bytes.Buffer{} + + defer func() { + OsExiter = fakeOsExiter + ErrWriter = fakeErrWriter + }() + + HandleExitCoder(Exit("", 2)) + + assert.True(t, called) + assert.Empty(t, ErrWriter.(*bytes.Buffer).String(), "expected no output for empty-message exit") +}