From 061c065cc1eef1b039f09076f4eb7cc612f45234 Mon Sep 17 00:00:00 2001 From: willem Date: Tue, 17 Sep 2024 11:39:58 +0200 Subject: [PATCH] binary: fix encoding attribute count when there are empty values (#658) Co-authored-by: Tulir Asokan --- binary/encoder.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/binary/encoder.go b/binary/encoder.go index 21e22cf8..eed07184 100644 --- a/binary/encoder.go +++ b/binary/encoder.go @@ -90,7 +90,7 @@ func (w *binaryEncoder) writeNode(n Node) { hasContent = 1 } - w.writeListStart(2*len(n.Attrs) + tagSize + hasContent) + w.writeListStart(2*w.countAttributes(n.Attrs) + tagSize + hasContent) w.writeString(n.Tag) w.writeAttributes(n.Attrs) if n.Content != nil { @@ -187,10 +187,6 @@ func (w *binaryEncoder) writeJID(jid types.JID) { } func (w *binaryEncoder) writeAttributes(attributes Attrs) { - if attributes == nil { - return - } - for key, val := range attributes { if val == "" || val == nil { continue @@ -201,6 +197,16 @@ func (w *binaryEncoder) writeAttributes(attributes Attrs) { } } +func (w *binaryEncoder) countAttributes(attributes Attrs) (count int) { + for _, val := range attributes { + if val == "" || val == nil { + continue + } + count += 1 + } + return +} + func (w *binaryEncoder) writeListStart(listSize int) { if listSize == 0 { w.pushByte(byte(token.ListEmpty))