diff --git a/client/object_get.go b/client/object_get.go index 0c68b18c..55fe239f 100644 --- a/client/object_get.go +++ b/client/object_get.go @@ -240,6 +240,9 @@ func (x *ObjectReader) Close() (*ResObjectGet, error) { // Read implements io.Reader of the object payload. func (x *ObjectReader) Read(p []byte) (int, error) { n, ok := x.readChunk(p) + + x.remainingPayloadLen -= n + if !ok { res, err := x.close(false) if err != nil { @@ -249,12 +252,10 @@ func (x *ObjectReader) Read(p []byte) (int, error) { return n, apistatus.ErrFromStatus(res.Status()) } - if n > x.remainingPayloadLen { + if x.remainingPayloadLen < 0 { return n, errors.New("payload size overflow") } - x.remainingPayloadLen -= n - return n, nil } @@ -615,6 +616,9 @@ func (x *ObjectRangeReader) Close() (*ResObjectRange, error) { // Read implements io.Reader of the object payload. func (x *ObjectRangeReader) Read(p []byte) (int, error) { n, ok := x.readChunk(p) + + x.remainingPayloadLen -= n + if !ok { res, err := x.close(false) if err != nil { @@ -624,12 +628,10 @@ func (x *ObjectRangeReader) Read(p []byte) (int, error) { return n, apistatus.ErrFromStatus(res.Status()) } - if n > x.remainingPayloadLen { + if x.remainingPayloadLen < 0 { return n, errors.New("payload range size overflow") } - x.remainingPayloadLen -= n - return n, nil }