diff --git a/src/debug/buildinfo/buildinfo_test.go b/src/debug/buildinfo/buildinfo_test.go index a0a816be17..499a92cf0a 100644 --- a/src/debug/buildinfo/buildinfo_test.go +++ b/src/debug/buildinfo/buildinfo_test.go @@ -18,6 +18,7 @@ import ( "runtime" "strings" "testing" + "time" ) var flagAll = flag.Bool("all", false, "test all supported GOOS/GOARCH platforms, instead of only the current platform") @@ -245,3 +246,31 @@ func TestReadFile(t *testing.T) { }) } } + +// TestReadFindBlob is a regression test for golang.org/issue/54968. +// +// The cause of issue 54968 is when the first buildInfoMagic is invalid, it +// enters an infinite loop. +func TestReadFindBlob(t *testing.T) { + t.Parallel() + + r := bytes.NewReader([]byte{0x4d, 0x5a, 0x64, 0x65, 0x61, 0x64, 0x74, 0x54, 0x59, 0x50, 0x45, 0x0, 0x0, 0x0, 0x0, 0x5, 0x0, 0x3a, 0x60, 0x5d, 0x60, 0x0, 0x0, 0x0, 0x0, 0x5c, 0x3f, 0x0, 0x40, 0x0, 0x0, 0x6c, 0x79, 0x4d, 0x44, 0x28, 0x20, 0x69, 0x6c, 0x6c, 0x4e, 0x6f, 0x53, 0x69, 0x6e, 0x67, 0x5b, 0x65, 0x6c, 0x44, 0x41, 0x28, 0x52, 0x6b, 0x6c, 0x4e, 0x6f, 0x53, 0x69, 0x64, 0x9, 0x0, 0x0, 0x0, 0x60, 0x0, 0x0, 0x0, 0x28, 0x6b, 0x6c, 0x4e, 0x6f, 0xef, 0x53, 0x69, 0x64, 0x0, 0x0, 0x6c, 0xed, 0x3c, 0xfa, 0x0, 0x2, 0x20, 0x20, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x5b, 0xf6, 0x0, 0xd3, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x6c, 0x69, 0x73, 0x74, 0x20, 0x7b, 0xa, 0x7d, 0xa, 0x6c, 0x69, 0x73, 0xff, 0x20, 0x7b, 0xa, 0xfe, 0xff, 0x7f, 0xff, 0x1, 0xe5, 0x20, 0x65, 0xff, 0x52, 0xff, 0x72, 0xa, 0x20, 0x3d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x55, 0x2a, 0x5b, 0x5d, 0x69, 0x6e, 0x74, 0x33, 0x32, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x78, 0x2f, 0x34, 0x54, 0x0, 0xa, 0x0, 0x20, 0x49, 0x0, 0x0, 0x23, 0x0, 0x20, 0x0, 0x0, 0x0, 0x2a, 0x5b, 0x5d, 0x2a, 0x66, 0xff, 0x20, 0x47, 0x6f, 0x20, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x69, 0x6e, 0x66, 0x3a, 0x40, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x69, 0x73, 0x2b, 0x20, 0x7b, 0xa, 0xfe, 0xff, 0xff, 0x20, 0xff, 0x20, 0xff, 0x65, 0xff, 0x52, 0xff, 0x72, 0xa, 0x20, 0x3d, 0x0, 0x0, 0x2a, 0x5b, 0x5d, 0x2a, 0x66, 0x0, 0x30, 0x5c, 0x30, 0x30, 0x30, 0x5c, 0x30, 0x30, 0x31, 0x5c, 0x30, 0x74, 0x65, 0x78, 0x74, 0x30, 0x30, 0x6c, 0x69, 0x73, 0x74, 0x20, 0x7b, 0x5c, 0x6e, 0x7d, 0x5c, 0x6e, 0x6c, 0x35, 0x35, 0x32, 0x32, 0x33, 0x30, 0x32, 0x37, 0x5c, 0x33, 0x37, 0x37, 0x20, 0x22, 0x2f, 0x22, 0xa, 0x20, 0x20, 0x0}) + + c := make(chan error) + go func() { + _, err := buildinfo.Read(r) + c <- err + }() + + select { + case err := <-c: + wantErr := "not a Go executable" + if err == nil { + t.Errorf("got error nil; want error containing %q", wantErr) + } else if errMsg := err.Error(); !strings.Contains(errMsg, wantErr) { + t.Errorf("got error %q; want error containing %q", errMsg, wantErr) + } + case <-time.After(time.Second): + t.Fatal("test timed out (endless loop in readRawBuildInfo?)") + } +}