binary icon indicating copy to clipboard operation
binary copied to clipboard

`ReadString` of `streamReader` may only read partial of the string with trailing '\0'

Open Jason5Lee opened this issue 3 years ago • 0 comments

Because the implementation of Slice method uses the Read method of io.Reader wrongly.

func (r *streamReader) Slice(n int) (buffer []byte, err error) {
	if n <= 10 {
		buffer = r.scratch[:n]
	} else {
		buffer = make([]byte, n, n)
	}

	_, err = r.Read(buffer)
	return
}

According to the doc of io.Reader, Read method reads up to len(p) bytes into p. It may actually read less. The unread part has the zero value.

The correct way is to use io.ReadFull.

This issue occurred in real life when I was trying to decode a bunch of the data from a file.

Jason5Lee avatar Aug 19 '22 08:08 Jason5Lee