Skip to content

v1.9.1 causes panic in writer #1383

@Luap99

Description

@Luap99

Found while updating to the lasted version:

panic: bufio.Scan: too many empty tokens without progressing

goroutine 27 [running]:
bufio.(*Scanner).Scan(0xc000653f28)
        /usr/lib/golang/src/bufio/scan.go:167 +0x82c
github.com/sirupsen/logrus.(*Entry).writerScanner(0x0?, 0xc000014058, 0xc000152140)
        /home/pholzing/go/src/github.com/containers/podman/vendor/github.com/sirupsen/logrus/writer.go:86 +0x11f
created by github.com/sirupsen/logrus.(*Entry).WriterLevel
        /home/pholzing/go/src/github.com/containers/podman/vendor/github.com/sirupsen/logrus/writer.go:57 +0x3d1

This seems to be caused by #1376 which does not look correct. the split function never tells the scanner to stop. Second it even changes the behaviour because it now no longer splits at newlines.

It easy to reproduce, just apply this test to the repo here:

diff --git a/writer_test.go b/writer_test.go
index 5c34927..a6f0b3c 100644
--- a/writer_test.go
+++ b/writer_test.go
@@ -1,10 +1,15 @@
 package logrus_test
 
 import (
+       "bytes"
        "log"
        "net/http"
+       "strings"
+       "testing"
+       "time"
 
        "github.com/sirupsen/logrus"
+       "github.com/stretchr/testify/assert"
 )
 
 func ExampleLogger_Writer_httpServer() {
@@ -32,3 +37,30 @@ func ExampleLogger_Writer_stdlib() {
        // Not logrus imported under the name `log`.
        log.SetOutput(logger.Writer())
 }
+
+func TestWriterSplitNewlines(t *testing.T) {
+       buf := bytes.NewBuffer(nil)
+       logger := logrus.New()
+       logger.Formatter = &logrus.TextFormatter{
+               DisableColors:    true,
+               DisableTimestamp: true,
+       }
+       logger.SetOutput(buf)
+       writer := logger.Writer()
+
+       const logNum = 10
+
+       for i := 0; i < logNum; i++ {
+               _, err := writer.Write([]byte("bar\nfoo\n"))
+               if err != nil {
+                       assert.NoError(t, err, "write.Write failed")
+               }
+       }
+       writer.Close()
+       // Test is flaky because it writes in another goroutine,
+       // we need to make sure to wait a bit so all write are done.
+       time.Sleep(500 * time.Millisecond)
+
+       lines := strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
+       assert.Len(t, lines, logNum*2, "logger printed incorrect number of lines")
+}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions