betterbinary: finally fix (well, a very hackish fix but should be ok) decoding events with way too many tags.

This commit is contained in:
fiatjaf
2025-08-27 01:19:08 -03:00
parent 80d0546ce6
commit 43d6814daf

View File

@@ -114,15 +114,24 @@ func Unmarshal(data []byte, evt *nostr.Event) (err error) {
evt.Sig = [64]byte(data[71:135]) evt.Sig = [64]byte(data[71:135])
const tagbase = 135 const tagbase = 135
tagsSectionLength := binary.LittleEndian.Uint16(data[tagbase:]) tagsSectionLength := int(binary.LittleEndian.Uint16(data[tagbase:]))
ntags := binary.LittleEndian.Uint16(data[tagbase+2:]) ntags := binary.LittleEndian.Uint16(data[tagbase+2:])
evt.Tags = make(nostr.Tags, ntags) evt.Tags = make(nostr.Tags, ntags)
prevOffset := 0
overflows := 0
for t := range evt.Tags { 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]) nitems := int(data[tagbase+offset])
tag := make(nostr.Tag, nitems) tag := make(nostr.Tag, nitems)
curr := int(tagbase + offset + 1) curr := tagbase + offset + 1
for i := range tag { for i := range tag {
length := int(binary.LittleEndian.Uint16(data[curr:])) length := int(binary.LittleEndian.Uint16(data[curr:]))
tag[i] = string(data[curr+2 : curr+2+length]) 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 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]) evt.Content = string(data[tagbase+tagsSectionLength+2 : tagbase+tagsSectionLength+2+contentLength])
return err return err