Skip to content

Commit 5e20694

Browse files
committed
TextFormatter: cover nil pointer method receivers
Extend the regression test for panicking Error and String methods to cover nil pointers with both pointer- and value-receiver implementations. This includes the value-receiver case that triggered the reported regression, such as *net.IP. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent e987a40 commit 5e20694

1 file changed

Lines changed: 38 additions & 6 deletions

File tree

text_formatter_test.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -702,24 +702,38 @@ func (s panicStringer) String() string { panic(string(s)) }
702702

703703
type nilError struct{}
704704

705-
func (*nilError) Error() string { panic("unexpected") }
705+
func (*nilError) Error() string { panic("boom") }
706+
707+
type nilValueError struct{}
708+
709+
func (nilValueError) Error() string { return "" }
706710

707711
type nilStringer struct{}
708712

709-
func (*nilStringer) String() string { panic("unexpected") }
713+
func (*nilStringer) String() string { panic("boom") }
714+
715+
type nilValueStringer struct{}
716+
717+
func (nilValueStringer) String() string { return "" }
710718

711719
// TestTextFormatterPanickingValue verifies panicking Error and Stringer
712720
// methods are recovered.
713721
//
714722
// regression test for https://github.com/sirupsen/logrus/issues/1580
715723
func TestTextFormatterPanickingValue(t *testing.T) {
716-
var nilErr *nilError
717-
var nilString *nilStringer
724+
var (
725+
nilErr *nilError
726+
nilValueErr *nilValueError
727+
nilString *nilStringer
728+
nilValueString *nilValueStringer
729+
)
718730

719731
tests := []struct {
720732
doc string
721733
value any
722734
want string
735+
736+
skipTiny bool
723737
}{
724738
{
725739
doc: "error",
@@ -732,15 +746,27 @@ func TestTextFormatterPanickingValue(t *testing.T) {
732746
want: `level=info test="%!v(PANIC=String method: boom)"` + "\n",
733747
},
734748
{
735-
doc: "nil error",
749+
doc: "nil pointer-receiver error",
736750
value: nilErr,
737751
want: `level=info test="<nil>"` + "\n",
738752
},
739753
{
740-
doc: "nil stringer",
754+
doc: "nil value-receiver error",
755+
value: nilValueErr,
756+
want: `level=info test="<nil>"` + "\n",
757+
skipTiny: true,
758+
},
759+
{
760+
doc: "nil pointer-receiver stringer",
741761
value: nilString,
742762
want: `level=info test="<nil>"` + "\n",
743763
},
764+
{
765+
doc: "nil value-receiver stringer",
766+
value: nilValueString,
767+
want: `level=info test="<nil>"` + "\n",
768+
skipTiny: true,
769+
},
744770
}
745771

746772
formatter := &TextFormatter{
@@ -750,6 +776,12 @@ func TestTextFormatterPanickingValue(t *testing.T) {
750776

751777
for _, tc := range tests {
752778
t.Run(tc.doc, func(t *testing.T) {
779+
if tc.skipTiny && runtime.Compiler == "tinygo" {
780+
// tinygo doesn't support t.Skip
781+
msg := "TinyGo does not panic when invoking a value-receiver method through a nil pointer"
782+
t.Log(msg+"\n\r--- SKIP:", t.Name(), "(0.00s)")
783+
return
784+
}
753785
got, err := formatter.Format(&Entry{
754786
Level: InfoLevel,
755787
Data: Fields{"test": tc.value},

0 commit comments

Comments
 (0)