diff options
author | Felix Lange <fjl@twurst.com> | 2015-03-25 23:46:29 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-03-25 23:46:29 +0800 |
commit | 181a21c67c8dafe027cd7fb43fe3f68eaf2a52ea (patch) | |
tree | 3c38cb00b62e8d1c68edab40b013f7972e4f5723 /rlp/encode.go | |
parent | ff5578fc715262cd8ae62e7d0f961a6e977a8727 (diff) | |
download | go-tangerine-181a21c67c8dafe027cd7fb43fe3f68eaf2a52ea.tar.gz go-tangerine-181a21c67c8dafe027cd7fb43fe3f68eaf2a52ea.tar.zst go-tangerine-181a21c67c8dafe027cd7fb43fe3f68eaf2a52ea.zip |
rlp: encode nil array pointers as empty list or string
Diffstat (limited to 'rlp/encode.go')
-rw-r--r-- | rlp/encode.go | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/rlp/encode.go b/rlp/encode.go index 289bc4eaa..6cf6776d6 100644 --- a/rlp/encode.go +++ b/rlp/encode.go @@ -87,6 +87,8 @@ func (e flatenc) EncodeRLP(out io.Writer) error { // To encode a pointer, the value being pointed to is encoded. For nil // pointers, Encode will encode the zero value of the type. A nil // pointer to a struct type always encodes as an empty RLP list. +// A nil pointer to an array encodes as an empty list (or empty string +// if the array has element type byte). // // Struct values are encoded as an RLP list of all their encoded // public fields. Recursive struct types are supported. @@ -532,21 +534,37 @@ func makePtrWriter(typ reflect.Type) (writer, error) { if err != nil { return nil, err } - zero := reflect.Zero(typ.Elem()) + + // determine nil pointer handler + var nilfunc func(*encbuf) error kind := typ.Elem().Kind() - writer := func(val reflect.Value, w *encbuf) error { - switch { - case !val.IsNil(): - return etypeinfo.writer(val.Elem(), w) - case kind == reflect.Struct: - // encoding the zero value of a struct could trigger + switch { + case kind == reflect.Array && isByte(typ.Elem().Elem()): + nilfunc = func(w *encbuf) error { + w.str = append(w.str, 0x80) + return nil + } + case kind == reflect.Struct || kind == reflect.Array: + nilfunc = func(w *encbuf) error { + // encoding the zero value of a struct/array could trigger // infinite recursion, avoid that. w.listEnd(w.list()) return nil - default: + } + default: + zero := reflect.Zero(typ.Elem()) + nilfunc = func(w *encbuf) error { return etypeinfo.writer(zero, w) } } + + writer := func(val reflect.Value, w *encbuf) error { + if val.IsNil() { + return nilfunc(w) + } else { + return etypeinfo.writer(val.Elem(), w) + } + } return writer, err } |