json-iterator / go

A high-performance 100% compatible drop-in replacement of "encoding/json"
http://jsoniter.com/migrate-from-go-std.html
MIT License
13.33k stars 1.02k forks source link

allocate a 512 byte buffer if none exists during Iterator.Reset() #669

Open Fusl opened 1 year ago

Fusl commented 1 year ago

Reproduction code:

iter := jsoniter.NewIterator(jsoniter.ConfigDefault).Reset(r)
t := iter.WhatIsNext()

This would block forever in the WhatIsNext function because no internal item.buf was allocated during Iterator.Reset() and Iterator.loadMore() will just endlessly try to fill the buffer here because the .Read() call with a nil buffer will immediately return without reading anything. https://github.com/json-iterator/go/blob/71ac16282d122fdd1e3a6d3e7f79b79b4cc3b50e/iter.go#L265-L279

The change here is to just allocate a 512 byte buffer if there wasn't one defined before. I believe that it can be safely reused without having to clear it because we're working with the iter.head and iter.tail everywhere we're making use of it.