bencode-go
bencode-go copied to clipboard
Out of memory when Decode is passed certain invalid data
When the data contains a large string length as in the example below, decodeString tries to allocate all of this memory upfront so the program crashes.
package main
import (
"bytes"
"fmt"
"github.com/jackpal/bencode-go"
)
func main() {
data := []byte("99999999999:")
buf := bytes.NewBuffer(data)
res, err := bencode.Decode(buf)
fmt.Println(res, err)
}
I think, it maybe supply an interface to allow the user to set the maximum string length, such as SetMaxStringLength(n int64), and when the length of a string is more than it, return an error.