[#55] object/transformer: Fix NPE in case of empty payload

In previous implementation payload size limiter panicked in case of payload
emptiness. It was caused by the component waiting for at least one write of
a part of the payload.

Fix NPE occurrence with internal initialization after the WriteHeader call.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-29 12:21:16 +03:00 committed by Alex Vanin
parent 867f1d772d
commit 1b5ac0f2ae

View file

@ -53,6 +53,8 @@ func NewPayloadSizeLimiter(maxSize uint64, targetInit TargetInitializer) ObjectT
func (s *payloadSizeLimiter) WriteHeader(hdr *object.RawObject) error { func (s *payloadSizeLimiter) WriteHeader(hdr *object.RawObject) error {
s.current = fromObject(hdr) s.current = fromObject(hdr)
s.initialize()
return nil return nil
} }
@ -214,15 +216,11 @@ func (s *payloadSizeLimiter) initializeLinking() {
} }
func (s *payloadSizeLimiter) writeChunk(chunk []byte) error { func (s *payloadSizeLimiter) writeChunk(chunk []byte) error {
// statement is true if: // statement is true if the previous write of bytes reached exactly the boundary.
// 1. the method is called for the first time; if s.written > 0 && s.written%s.maxSize == 0 {
// 2. the previous write of bytes reached exactly the boundary. // we need to release current object
if s.written%s.maxSize == 0 { if _, err := s.release(false); err != nil {
// if 2. we need to release current object return errors.Wrap(err, "could not release object")
if s.written > 0 {
if _, err := s.release(false); err != nil {
return errors.Wrap(err, "could not release object")
}
} }
// initialize another object // initialize another object