From f59bd61282bde0108e79c73399461289a63fae91 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 18 Jan 2024 12:50:46 +0100 Subject: [PATCH] rlp: fix off by one-flaw, make tests more idiomatic --- rlp/raw.go | 4 ++-- rlp/raw_test.go | 25 +++++++++++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/rlp/raw.go b/rlp/raw.go index cdd3ec9c18..3bf7f41de9 100644 --- a/rlp/raw.go +++ b/rlp/raw.go @@ -336,8 +336,8 @@ func EndList(buf []byte, offset int) []byte { return buf } headerSize := intsize(uint64(contentSize)) + 1 - // shift the content for room of list header - buf = append(buf[:offset+headerSize], buf[offset:]...) + // shift the content for room of list header (minus the one byte already written) + buf = append(buf[:offset+headerSize-1], buf[offset:]...) // write list header. OBS! This call ignores the return value, // since the append operation is performed on buf[:offset], and we // already just moved the content, we know that the append operation diff --git a/rlp/raw_test.go b/rlp/raw_test.go index c44dea6083..2ad06b1014 100644 --- a/rlp/raw_test.go +++ b/rlp/raw_test.go @@ -340,22 +340,21 @@ func TestAppendString(t *testing.T) { func TestEndList(t *testing.T) { tests := []struct { input interface{} - slice []byte output string }{ - {[]interface{}{}, nil, "C0"}, - {[]interface{}{1, 2, 3}, nil, "C3010203"}, + {[]interface{}{}, "C0"}, + {[]interface{}{1, 2, 3}, "C3010203"}, { // [ [], [[]], [ [], [[]] ] ] - []interface{}{[]interface{}{}, []interface{}{[]interface{}{}}, []interface{}{[]interface{}{}, []interface{}{[]interface{}{}}}}, nil, + []interface{}{[]interface{}{}, []interface{}{[]interface{}{}}, []interface{}{[]interface{}{}, []interface{}{[]interface{}{}}}}, "C7C0C1C0C3C0C1C0", }, { - []interface{}{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo"}, nil, + []interface{}{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo"}, "F83C836161618362626283636363836464648365656583666666836767678368686883696969836A6A6A836B6B6B836C6C6C836D6D6D836E6E6E836F6F6F", }, { - []interface{}{1, 0xFFFFFF, []interface{}{[]interface{}{4, 5, 5}}, "abc"}, nil, + []interface{}{1, 0xFFFFFF, []interface{}{[]interface{}{4, 5, 5}}, "abc"}, "CE0183FFFFFFC4C304050583616263", }, {[]interface{}{ @@ -390,7 +389,7 @@ func TestEndList(t *testing.T) { []interface{}{"asdf", "qwer", "zxcv"}, []interface{}{"asdf", "qwer", "zxcv"}, []interface{}{"asdf", "qwer", "zxcv"}, - []interface{}{"asdf", "qwer", "zxcv"}}, nil, + []interface{}{"asdf", "qwer", "zxcv"}}, "F90200CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376CF84617364668471776572847A786376"}, } @@ -398,7 +397,8 @@ func TestEndList(t *testing.T) { encode = func(buf []byte, v interface{}) []byte { switch v := v.(type) { case []interface{}: - offset := len(buf) + var offset int + buf, offset = StartList(buf) for _, c := range v { buf = encode(buf, c) } @@ -416,10 +416,11 @@ func TestEndList(t *testing.T) { } } - for _, test := range tests { - x := encode(test.slice, test.input) - if !bytes.Equal(x, unhex(test.output)) { - t.Errorf("TestEndList(%v, %d): got %x, want %s", test.slice, test.input, x, test.output) + for i, test := range tests { + have := encode(nil, test.input) + want := unhex(test.output) + if !bytes.Equal(have, want) { + t.Errorf("test %d: input %v \n\thave %x\n\twant %x", i, test.input, have, want) } } }