Skip to content

pktline.ReadLine() isn't safe for concurrent usage #1766

@onee-only

Description

@onee-only

Bug Description

When pktline.ReadLine()is called, the function returns its buffer to the pool while keeping the reference of the buffer. This can lead to memory corruption, as the buffer may be reused and overwritten by other goroutines at any time.

func ReadLine(r io.Reader) (l int, p []byte, err error) {
buf := GetBuffer()
defer PutBuffer(buf)
l, err = Read(r, (*buf)[:])
if l < LenSize {
return l, nil, err
}
return l, (*buf)[LenSize:l], err
}

go-git Version

48f817f

Steps to Reproduce

func TestConcurrentReadLine(t *testing.T) {
	var wg sync.WaitGroup
	for n := range 30 {
		wg.Add(1)
		go func() {
			defer wg.Done()

			data := strconv.Itoa(n)

			var buf bytes.Buffer
			_, err := pktline.Write(&buf, []byte(data))
			require.NoError(t, err)

			_, p, err := pktline.ReadLine(&buf)
			require.NoError(t, err)
			assert.Equal(t, data, string(p))

		}()
	}
	wg.Wait()
}

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions