File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -64,6 +64,8 @@ func NewReader(rd io.Reader) *Reader {
64
64
return NewReaderSize (rd , defaultBufSize )
65
65
}
66
66
67
+ var errNegativeRead = errors .New ("bufio: reader returned negative count from Read" )
68
+
67
69
// fill reads a new chunk into the buffer.
68
70
func (b * Reader ) fill () {
69
71
// Slide existing data to beginning.
@@ -75,6 +77,9 @@ func (b *Reader) fill() {
75
77
76
78
// Read new data.
77
79
n , e := b .rd .Read (b .buf [b .w :])
80
+ if n < 0 {
81
+ panic (errNegativeRead )
82
+ }
78
83
b .w += n
79
84
if e != nil {
80
85
b .err = e
@@ -282,6 +287,9 @@ func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
282
287
// of the line. The returned buffer is only valid until the next call to
283
288
// ReadLine. ReadLine either returns a non-nil line or it returns an error,
284
289
// never both.
290
+ //
291
+ // The text returned from ReadLine does not include the line end ("\r\n" or "\n").
292
+ // No indication or error is given if the input ends without a final line end.
285
293
func (b * Reader ) ReadLine () (line []byte , isPrefix bool , err error ) {
286
294
line , err = b .ReadSlice ('\n' )
287
295
if err == ErrBufferFull {
Original file line number Diff line number Diff line change @@ -939,6 +939,29 @@ func (w *writeCountingDiscard) Write(p []byte) (int, error) {
939
939
return len (p ), nil
940
940
}
941
941
942
+ type negativeReader int
943
+
944
+ func (r * negativeReader ) Read ([]byte ) (int , error ) { return - 1 , nil }
945
+
946
+ func TestNegativeRead (t * testing.T ) {
947
+ // should panic with a description pointing at the reader, not at itself.
948
+ // (should NOT panic with slice index error, for example.)
949
+ b := NewReader (new (negativeReader ))
950
+ defer func () {
951
+ switch err := recover ().(type ) {
952
+ case nil :
953
+ t .Fatal ("read did not panic" )
954
+ case error :
955
+ if ! strings .Contains (err .Error (), "reader returned negative count from Read" ) {
956
+ t .Fatal ("wrong panic: %v" , err )
957
+ }
958
+ default :
959
+ t .Fatalf ("unexpected panic value: %T(%v)" , err , err )
960
+ }
961
+ }()
962
+ b .Read (make ([]byte , 100 ))
963
+ }
964
+
942
965
// An onlyReader only implements io.Reader, no matter what other methods the underlying implementation may have.
943
966
type onlyReader struct {
944
967
r io.Reader
You can’t perform that action at this time.
0 commit comments