From 1b5ac0f2aee3c1efe24140aa2506415049d7925c Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 29 Sep 2020 12:21:16 +0300 Subject: [PATCH] [#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 --- .../object_manager/transformer/transformer.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/services/object_manager/transformer/transformer.go b/pkg/services/object_manager/transformer/transformer.go index d7d43957b1..19c4008389 100644 --- a/pkg/services/object_manager/transformer/transformer.go +++ b/pkg/services/object_manager/transformer/transformer.go @@ -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,15 +216,11 @@ 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 { - if _, err := s.release(false); err != nil { - return errors.Wrap(err, "could not release object") - } + // 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