bencode-go icon indicating copy to clipboard operation
bencode-go copied to clipboard

Out of memory when Decode is passed certain invalid data

Open jakobvarmose opened this issue 8 years ago • 1 comments

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)
}

jakobvarmose avatar Feb 09 '17 01:02 jakobvarmose

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.

xgfone avatar Jul 29 '17 02:07 xgfone