From 964c3edb3f44306378cdb40adffbc0b68318d1d8 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 2 Aug 2023 10:55:05 +0300 Subject: [PATCH] [#51] *: Do not panic in StableSize() After #49 it can be called on nil structures. Signed-off-by: Evgenii Stratonikov --- netmap/marshal.go | 16 ++++++++++++++++ status/marshal.go | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/netmap/marshal.go b/netmap/marshal.go index bf559ce..4f2c1ce 100644 --- a/netmap/marshal.go +++ b/netmap/marshal.go @@ -65,6 +65,10 @@ func (f *Filter) StableMarshal(buf []byte) []byte { } func (f *Filter) StableSize() (size int) { + if f == nil { + return 0 + } + size += protoutil.StringSize(nameFilterField, f.name) size += protoutil.StringSize(keyFilterField, f.key) size += protoutil.EnumSize(opFilterField, int32(f.op)) @@ -101,6 +105,10 @@ func (s *Selector) StableMarshal(buf []byte) []byte { } func (s *Selector) StableSize() (size int) { + if s == nil { + return 0 + } + size += protoutil.StringSize(nameSelectorField, s.name) size += protoutil.UInt32Size(countSelectorField, s.count) size += protoutil.EnumSize(countSelectorField, int32(s.clause)) @@ -132,6 +140,10 @@ func (r *Replica) StableMarshal(buf []byte) []byte { } func (r *Replica) StableSize() (size int) { + if r == nil { + return 0 + } + size += protoutil.UInt32Size(countReplicaField, r.count) size += protoutil.StringSize(selectorReplicaField, r.selector) @@ -173,6 +185,10 @@ func (p *PlacementPolicy) StableMarshal(buf []byte) []byte { } func (p *PlacementPolicy) StableSize() (size int) { + if p == nil { + return 0 + } + for i := range p.replicas { size += protoutil.NestedStructureSize(replicasPolicyField, &p.replicas[i]) } diff --git a/status/marshal.go b/status/marshal.go index 1868a43..2908e0d 100644 --- a/status/marshal.go +++ b/status/marshal.go @@ -30,6 +30,10 @@ func (x *Detail) StableMarshal(buf []byte) []byte { } func (x *Detail) StableSize() (size int) { + if x == nil { + return 0 + } + size += protoutil.UInt32Size(detailIDFNum, x.id) size += protoutil.BytesSize(detailValueFNum, x.val)