diff --git a/bencode/decode.go b/bencode/decode.go index 27ff898c..2d27d683 100644 --- a/bencode/decode.go +++ b/bencode/decode.go @@ -5,6 +5,7 @@ import ( "bytes" "errors" "io" + "math/big" "reflect" "runtime" "strconv" @@ -503,7 +504,7 @@ func (d *decoder) parse_value_interface() (interface{}, bool) { } } -func (d *decoder) parse_int_interface() interface{} { +func (d *decoder) parse_int_interface() (ret interface{}) { start := d.offset - 1 d.read_until('e') if d.buf.Len() == 0 { @@ -514,9 +515,23 @@ func (d *decoder) parse_int_interface() interface{} { } n, err := strconv.ParseInt(d.buf.String(), 10, 64) - check_for_int_parse_error(err, start) + if ne, ok := err.(*strconv.NumError); ok && ne.Err == strconv.ErrRange { + i := new(big.Int) + _, ok := i.SetString(d.buf.String(), 10) + if !ok { + panic(&SyntaxError{ + Offset: start, + What: errors.New("failed to parse integer"), + }) + } + ret = i + } else { + check_for_int_parse_error(err, start) + ret = n + } + d.buf.Reset() - return n + return } func (d *decoder) parse_string_interface() interface{} { diff --git a/bencode/decode_test.go b/bencode/decode_test.go index d5aa8e9c..396fa062 100644 --- a/bencode/decode_test.go +++ b/bencode/decode_test.go @@ -3,6 +3,8 @@ package bencode import ( "bytes" "io" + "log" + "math/big" "reflect" "testing" @@ -24,6 +26,10 @@ var random_decode_tests = []random_decode_test{ []interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"}}, {"ldedee", []interface{}{map[string]interface{}{}, map[string]interface{}{}}}, {"le", []interface{}{}}, + {"i604919719469385652980544193299329427705624352086e", func() *big.Int { + ret, _ := big.NewInt(-1).SetString("604919719469385652980544193299329427705624352086", 10) + return ret + }()}, } func TestRandomDecode(t *testing.T) {