]> Sergey Matveev's repositories - btrtrc.git/commitdiff
bencode: Decode singleton lists of the expected type
authorMatt Joiner <anacrolix@gmail.com>
Thu, 13 Jun 2019 03:07:37 +0000 (13:07 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 13 Jun 2019 03:07:37 +0000 (13:07 +1000)
Fixes #297.

bencode/decode.go

index 081a44a715ea7fc7e31d95790a1c8f39a863b9ae..8b22fa734f6f4f19fc26c6d1260d3210165b18e5 100644 (file)
@@ -344,12 +344,23 @@ func (d *Decoder) parseDict(v reflect.Value) error {
 
 func (d *Decoder) parseList(v reflect.Value) error {
        switch v.Kind() {
-       case reflect.Array, reflect.Slice:
        default:
-               return &UnmarshalTypeError{
-                       Value: "list",
-                       Type:  v.Type(),
+               // If the list is a singleton of the expected type, use that value. See
+               // https://github.com/anacrolix/torrent/issues/297.
+               l := reflect.New(reflect.SliceOf(v.Type()))
+               if err := d.parseList(l.Elem()); err != nil {
+                       return err
+               }
+               if l.Elem().Len() != 1 {
+                       return &UnmarshalTypeError{
+                               Value: "list",
+                               Type:  v.Type(),
+                       }
                }
+               v.Set(l.Elem().Index(0))
+               return nil
+       case reflect.Array, reflect.Slice:
+               // We can work with this. Normal case, fallthrough.
        }
 
        i := 0