rlp: fixes for two corner cases and documentation (#19527)

These changes fix two corner cases related to internal handling of types
in package rlp: The "tail" struct tag can only be applied to the last field.
The check for this was wrong and didn't allow for private fields after the
field with the tag. Unsupported types (e.g. structs containing int) which
implement either the Encoder or Decoder interface but not both 
couldn't be encoded/decoded.

Also fixes #19367
This commit is contained in:
Felix Lange
2019-05-14 15:09:56 +02:00
committed by GitHub
parent 184af72e4e
commit 8deec2e45a
5 changed files with 111 additions and 57 deletions

View File

@ -347,6 +347,12 @@ type tailUint struct {
Tail []uint `rlp:"tail"`
}
type tailPrivateFields struct {
A uint
Tail []uint `rlp:"tail"`
x, y bool
}
var (
veryBigInt = big.NewInt(0).Add(
big.NewInt(0).Lsh(big.NewInt(0xFFFFFFFFFFFFFF), 16),
@ -510,6 +516,11 @@ var decodeTests = []decodeTest{
ptr: new(tailRaw),
value: tailRaw{A: 1, Tail: []RawValue{}},
},
{
input: "C3010203",
ptr: new(tailPrivateFields),
value: tailPrivateFields{A: 1, Tail: []uint{2, 3}},
},
// struct tag "-"
{
@ -691,6 +702,27 @@ func TestDecoderInByteSlice(t *testing.T) {
}
}
type unencodableDecoder func()
func (f *unencodableDecoder) DecodeRLP(s *Stream) error {
if _, err := s.List(); err != nil {
return err
}
if err := s.ListEnd(); err != nil {
return err
}
*f = func() {}
return nil
}
func TestDecoderFunc(t *testing.T) {
var x func()
if err := DecodeBytes([]byte{0xC0}, (*unencodableDecoder)(&x)); err != nil {
t.Fatal(err)
}
x()
}
func ExampleDecode() {
input, _ := hex.DecodeString("C90A1486666F6F626172")