Skip to content

Commit

Permalink
binary: fix encoding attribute count when there are empty values (tul…
Browse files Browse the repository at this point in the history
…ir#658)

Co-authored-by: Tulir Asokan <[email protected]>
  • Loading branch information
2 people authored and crazycodezombie committed Sep 30, 2024
1 parent c1381e1 commit 47e8b27
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions binary/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down

0 comments on commit 47e8b27

Please sign in to comment.