app/vminsert: really fix #60

ReadLinesBlock may accept dstBuf with non-zero length. In this case the last line without trailing newline isn't read.
Fix this by comparing len(dstBuf) to 0 instead of its original length.
This commit is contained in:
Aliaksandr Valialkin 2019-06-07 23:36:58 +03:00
parent 0b78d228d2
commit f4252f87e6
2 changed files with 2 additions and 2 deletions

View file

@ -20,7 +20,6 @@ const defaultBlockSize = 64 * 1024
// //
// Returns (dstBuf, tailBuf). // Returns (dstBuf, tailBuf).
func ReadLinesBlock(r io.Reader, dstBuf, tailBuf []byte) ([]byte, []byte, error) { func ReadLinesBlock(r io.Reader, dstBuf, tailBuf []byte) ([]byte, []byte, error) {
origDstBufLen := len(dstBuf)
if cap(dstBuf) < defaultBlockSize { if cap(dstBuf) < defaultBlockSize {
dstBuf = bytesutil.Resize(dstBuf, defaultBlockSize) dstBuf = bytesutil.Resize(dstBuf, defaultBlockSize)
} }
@ -33,7 +32,7 @@ again:
if err == nil { if err == nil {
return dstBuf, tailBuf, fmt.Errorf("no forward progress made") return dstBuf, tailBuf, fmt.Errorf("no forward progress made")
} }
if err == io.EOF && len(dstBuf) > origDstBufLen { if err == io.EOF && len(dstBuf) > 0 {
// Missing newline in the end of stream. This is OK, // Missing newline in the end of stream. This is OK,
/// so suppress io.EOF for now. It will be returned during the next /// so suppress io.EOF for now. It will be returned during the next
// call to ReadLinesBlock. // call to ReadLinesBlock.

View file

@ -79,6 +79,7 @@ func TestReadLineBlockSuccessSingleByteReader(t *testing.T) {
f("\nfoo", "", "") f("\nfoo", "", "")
f("foo\nbar", "foo", "") f("foo\nbar", "foo", "")
f("foo\nbar\nbaz", "foo", "") f("foo\nbar\nbaz", "foo", "")
f("foo", "foo", "")
// The maximum line size // The maximum line size
b := make([]byte, maxLineSize+10) b := make([]byte, maxLineSize+10)