[#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 {
s.current = fromObject(hdr)
s.initialize()
return nil
}
@ -214,16 +216,12 @@ func (s *payloadSizeLimiter) initializeLinking() {
}
func (s *payloadSizeLimiter) writeChunk(chunk []byte) error {
// statement is true if:
// 1. the method is called for the first time;
// 2. the previous write of bytes reached exactly the boundary.
if s.written%s.maxSize == 0 {
// if 2. we need to release current object
if s.written > 0 {
// statement is true if the previous write of bytes reached exactly the boundary.
if s.written > 0 && s.written%s.maxSize == 0 {
// we need to release current object
if _, err := s.release(false); err != nil {
return errors.Wrap(err, "could not release object")
}
}
// initialize another object
s.initialize()