2
0
mirror of synced 2025-02-24 06:38:14 +00:00

bencode: When decoding a dict, if the final key is missing its value, don't treat that as an error.

I think I'm swayed here by the robustness principle/Postel's Law.
This commit is contained in:
Matt Joiner 2015-10-02 00:13:43 +10:00
parent b7a8bb7570
commit 28c1536daf
2 changed files with 13 additions and 18 deletions

View File

@ -264,10 +264,7 @@ func (d *decoder) parse_dict(v reflect.Value) {
} else { } else {
_, ok := d.parse_value_interface() _, ok := d.parse_value_interface()
if !ok { if !ok {
panic(&SyntaxError{ return
Offset: d.offset,
What: errors.New("unexpected end of dict, no matching value for a given key"),
})
} }
continue continue
} }
@ -275,10 +272,7 @@ func (d *decoder) parse_dict(v reflect.Value) {
// now we need to actually parse it // now we need to actually parse it
if !d.parse_value(valuev) { if !d.parse_value(valuev) {
panic(&SyntaxError{ return
Offset: d.offset,
What: errors.New("unexpected end of dict, no matching value for a given key"),
})
} }
if v.Kind() == reflect.Map { if v.Kind() == reflect.Map {
@ -414,8 +408,8 @@ func (d *decoder) parse_unmarshaler(v reflect.Value) bool {
return false return false
} }
// returns true if there was a value and it's now stored in 'v', otherwise there // Returns true if there was a value and it's now stored in 'v', otherwise
// was an end symbol ("e") and no value was stored // there was an end symbol ("e") and no value was stored.
func (d *decoder) parse_value(v reflect.Value) bool { func (d *decoder) parse_value(v reflect.Value) bool {
// we support one level of indirection at the moment // we support one level of indirection at the moment
if v.Kind() == reflect.Ptr { if v.Kind() == reflect.Ptr {
@ -576,10 +570,7 @@ func (d *decoder) parse_dict_interface() interface{} {
valuei, ok := d.parse_value_interface() valuei, ok := d.parse_value_interface()
if !ok { if !ok {
panic(&SyntaxError{ break
Offset: d.offset,
What: errors.New("unexpected end of dict, no matching value for a given key"),
})
} }
dict[key] = valuei dict[key] = valuei

View File

@ -7,6 +7,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -29,6 +30,12 @@ var random_decode_tests = []random_decode_test{
ret, _ := big.NewInt(-1).SetString("604919719469385652980544193299329427705624352086", 10) ret, _ := big.NewInt(-1).SetString("604919719469385652980544193299329427705624352086", 10)
return ret return ret
}()}, }()},
{"d1:rd6:\xd4/\xe2F\x00\x01e1:t3:\x9a\x87\x011:v4:TR%=1:y1:re", map[string]interface{}{
"r": map[string]interface{}{},
"t": "\x9a\x87\x01",
"v": "TR%=",
"y": "r",
}},
} }
func TestRandomDecode(t *testing.T) { func TestRandomDecode(t *testing.T) {
@ -39,10 +46,7 @@ func TestRandomDecode(t *testing.T) {
t.Error(err, test.data) t.Error(err, test.data)
continue continue
} }
if !reflect.DeepEqual(test.expected, value) { assert.EqualValues(t, test.expected, value)
t.Errorf("got: %v (%T), expected: %v (%T)\n",
value, value, test.expected, test.expected)
}
} }
} }