From 43d6814daf542a185c6a0d5f28e741b3c5c3de3a Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 27 Aug 2025 01:19:08 -0300 Subject: [PATCH] betterbinary: finally fix (well, a very hackish fix but should be ok) decoding events with way too many tags. --- eventstore/codec/betterbinary/codec.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/eventstore/codec/betterbinary/codec.go b/eventstore/codec/betterbinary/codec.go index 5c24841..8417ef4 100644 --- a/eventstore/codec/betterbinary/codec.go +++ b/eventstore/codec/betterbinary/codec.go @@ -114,15 +114,24 @@ func Unmarshal(data []byte, evt *nostr.Event) (err error) { evt.Sig = [64]byte(data[71:135]) const tagbase = 135 - tagsSectionLength := binary.LittleEndian.Uint16(data[tagbase:]) + tagsSectionLength := int(binary.LittleEndian.Uint16(data[tagbase:])) ntags := binary.LittleEndian.Uint16(data[tagbase+2:]) evt.Tags = make(nostr.Tags, ntags) + prevOffset := 0 + overflows := 0 for t := range evt.Tags { - offset := binary.LittleEndian.Uint16(data[tagbase+4+t*2:]) + offset := int(binary.LittleEndian.Uint16(data[tagbase+4+t*2:])) + if offset < prevOffset { + // we've reached the u16 overflow for the tag offsets, so we do this amazing hack + overflows++ + } + prevOffset = offset + offset += (1 << 16) * overflows + nitems := int(data[tagbase+offset]) tag := make(nostr.Tag, nitems) - curr := int(tagbase + offset + 1) + curr := tagbase + offset + 1 for i := range tag { length := int(binary.LittleEndian.Uint16(data[curr:])) tag[i] = string(data[curr+2 : curr+2+length]) @@ -131,7 +140,8 @@ func Unmarshal(data []byte, evt *nostr.Event) (err error) { evt.Tags[t] = tag } - contentLength := binary.LittleEndian.Uint16(data[tagbase+tagsSectionLength:]) + tagsSectionLength += (1 << 16) * overflows + contentLength := int(binary.LittleEndian.Uint16(data[tagbase+tagsSectionLength:])) evt.Content = string(data[tagbase+tagsSectionLength+2 : tagbase+tagsSectionLength+2+contentLength]) return err