]> Sergey Matveev's repositories - btrtrc.git/commitdiff
bencode: When decoding a dict, if the final key is missing its value, don't treat...
authorMatt Joiner <anacrolix@gmail.com>
Thu, 1 Oct 2015 14:13:43 +0000 (00:13 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 1 Oct 2015 14:13:43 +0000 (00:13 +1000)
I think I'm swayed here by the robustness principle/Postel's Law.

bencode/decode.go
bencode/decode_test.go

index 2d27d683aa9b07834690a335b69f4b3f4e6b5230..f90783413172a1a7804a01694200e9e6dddecf7b 100644 (file)
@@ -264,10 +264,7 @@ func (d *decoder) parse_dict(v reflect.Value) {
                        } else {
                                _, ok := d.parse_value_interface()
                                if !ok {
-                                       panic(&SyntaxError{
-                                               Offset: d.offset,
-                                               What:   errors.New("unexpected end of dict, no matching value for a given key"),
-                                       })
+                                       return
                                }
                                continue
                        }
@@ -275,10 +272,7 @@ func (d *decoder) parse_dict(v reflect.Value) {
 
                // now we need to actually parse it
                if !d.parse_value(valuev) {
-                       panic(&SyntaxError{
-                               Offset: d.offset,
-                               What:   errors.New("unexpected end of dict, no matching value for a given key"),
-                       })
+                       return
                }
 
                if v.Kind() == reflect.Map {
@@ -414,8 +408,8 @@ func (d *decoder) parse_unmarshaler(v reflect.Value) bool {
        return false
 }
 
-// returns true if there was a value and it's now stored in 'v', otherwise there
-// was an end symbol ("e") and no value was stored
+// Returns true if there was a value and it's now stored in 'v', otherwise
+// there was an end symbol ("e") and no value was stored.
 func (d *decoder) parse_value(v reflect.Value) bool {
        // we support one level of indirection at the moment
        if v.Kind() == reflect.Ptr {
@@ -576,10 +570,7 @@ func (d *decoder) parse_dict_interface() interface{} {
 
                valuei, ok := d.parse_value_interface()
                if !ok {
-                       panic(&SyntaxError{
-                               Offset: d.offset,
-                               What:   errors.New("unexpected end of dict, no matching value for a given key"),
-                       })
+                       break
                }
 
                dict[key] = valuei
index a4bf84b49a0c89a84c28eb037236e5f630968b49..d519b8a6a0bdc24edf2bc77ab66938c7ad708f47 100644 (file)
@@ -7,6 +7,7 @@ import (
        "reflect"
        "testing"
 
+       "github.com/stretchr/testify/assert"
        "github.com/stretchr/testify/require"
 )
 
@@ -29,6 +30,12 @@ var random_decode_tests = []random_decode_test{
                ret, _ := big.NewInt(-1).SetString("604919719469385652980544193299329427705624352086", 10)
                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) {
@@ -39,10 +46,7 @@ func TestRandomDecode(t *testing.T) {
                        t.Error(err, test.data)
                        continue
                }
-               if !reflect.DeepEqual(test.expected, value) {
-                       t.Errorf("got: %v (%T), expected: %v (%T)\n",
-                               value, value, test.expected, test.expected)
-               }
+               assert.EqualValues(t, test.expected, value)
        }
 }