Skip to content

Commit 0b00042

Browse files
committed
Merge pull request #269 from go-sql-driver/buffer
buffer: return io.ErrUnexpectedEOF
2 parents c56ef14 + d89db8a commit 0b00042

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

buffer.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ func newBuffer(rd io.Reader) buffer {
3434

3535
// fill reads into the buffer until at least _need_ bytes are in it
3636
func (b *buffer) fill(need int) error {
37+
n := b.length
38+
3739
// move existing data to the beginning
38-
if b.length > 0 && b.idx > 0 {
39-
copy(b.buf[0:b.length], b.buf[b.idx:])
40+
if n > 0 && b.idx > 0 {
41+
copy(b.buf[0:n], b.buf[b.idx:])
4042
}
4143

4244
// grow buffer if necessary
@@ -52,19 +54,27 @@ func (b *buffer) fill(need int) error {
5254
b.idx = 0
5355

5456
for {
55-
n, err := b.rd.Read(b.buf[b.length:])
56-
b.length += n
57+
nn, err := b.rd.Read(b.buf[n:])
58+
n += nn
5759

58-
if err == nil {
59-
if b.length < need {
60+
switch err {
61+
case nil:
62+
if n < need {
6063
continue
6164
}
65+
b.length = n
6266
return nil
67+
68+
case io.EOF:
69+
if n >= need {
70+
b.length = n
71+
return nil
72+
}
73+
return io.ErrUnexpectedEOF
74+
75+
default:
76+
return err
6377
}
64-
if b.length >= need && err == io.EOF {
65-
return nil
66-
}
67-
return err
6878
}
6979
}
7080

0 commit comments

Comments
 (0)