Skip to content

Commit 93e858f

Browse files
committed
rlp: remove support for signed integer types
There is no agreement on how to encode negative integers across implementations. cpp-ethereum doesn't support them either.
1 parent 4f12f06 commit 93e858f

3 files changed

Lines changed: 29 additions & 52 deletions

File tree

‎rlp/decode.go‎

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Decoder interface {
5454
// To decode into a Go string, the input must be an RLP string. The
5555
// bytes are taken as-is and will not necessarily be valid UTF-8.
5656
//
57-
// To decode into an integer type, the input must also be an RLP
57+
// To decode into an unsigned integer type, the input must also be an RLP
5858
// string. The bytes are interpreted as a big endian representation of
5959
// the integer. If the RLP string is larger than the bit size of the
6060
// type, Decode will return an error. Decode also supports *big.Int.
@@ -66,8 +66,9 @@ type Decoder interface {
6666
// []interface{}, for RLP lists
6767
// []byte, for RLP strings
6868
//
69-
// Non-empty interface types are not supported, nor are bool, float32,
70-
// float64, maps, channel types and functions.
69+
// Non-empty interface types are not supported, nor are booleans,
70+
// signed integers, floating point numbers, maps, channels and
71+
// functions.
7172
func Decode(r io.Reader, val interface{}) error {
7273
return NewStream(r).Decode(val)
7374
}
@@ -97,8 +98,8 @@ func makeDecoder(typ reflect.Type) (dec decoder, err error) {
9798
return decodeBigInt, nil
9899
case typ.AssignableTo(bigInt):
99100
return decodeBigIntNoPtr, nil
100-
case isInteger(kind):
101-
return makeNumDecoder(typ), nil
101+
case isUint(kind):
102+
return decodeUint, nil
102103
case kind == reflect.String:
103104
return decodeString, nil
104105
case kind == reflect.Slice || kind == reflect.Array:
@@ -114,30 +115,6 @@ func makeDecoder(typ reflect.Type) (dec decoder, err error) {
114115
}
115116
}
116117

117-
func makeNumDecoder(typ reflect.Type) decoder {
118-
kind := typ.Kind()
119-
switch {
120-
case kind <= reflect.Int64:
121-
return decodeInt
122-
case kind <= reflect.Uint64:
123-
return decodeUint
124-
default:
125-
panic("fallthrough")
126-
}
127-
}
128-
129-
func decodeInt(s *Stream, val reflect.Value) error {
130-
typ := val.Type()
131-
num, err := s.uint(typ.Bits())
132-
if err == errUintOverflow {
133-
return decodeError{"input string too long", typ}
134-
} else if err != nil {
135-
return err
136-
}
137-
val.SetInt(int64(num))
138-
return nil
139-
}
140-
141118
func decodeUint(s *Stream, val reflect.Value) error {
142119
typ := val.Type()
143120
num, err := s.uint(typ.Bits())

‎rlp/decode_test.go‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func TestDecodeErrors(t *testing.T) {
171171
t.Errorf("Decode(r, new(chan bool)) error mismatch, got %q, want %q", err, expectErr)
172172
}
173173

174-
if err := Decode(r, new(int)); err != io.EOF {
174+
if err := Decode(r, new(uint)); err != io.EOF {
175175
t.Errorf("Decode(r, new(int)) error mismatch, got %q, want %q", err, io.EOF)
176176
}
177177
}
@@ -184,12 +184,12 @@ type decodeTest struct {
184184
}
185185

186186
type simplestruct struct {
187-
A int
187+
A uint
188188
B string
189189
}
190190

191191
type recstruct struct {
192-
I int
192+
I uint
193193
Child *recstruct
194194
}
195195

@@ -202,7 +202,7 @@ var (
202202

203203
var (
204204
sharedByteArray [5]byte
205-
sharedPtr = new(*int)
205+
sharedPtr = new(*uint)
206206
)
207207

208208
var decodeTests = []decodeTest{
@@ -217,13 +217,13 @@ var decodeTests = []decodeTest{
217217
{input: "C0", ptr: new(uint32), error: ErrExpectedString.Error()},
218218

219219
// slices
220-
{input: "C0", ptr: new([]int), value: []int{}},
221-
{input: "C80102030405060708", ptr: new([]int), value: []int{1, 2, 3, 4, 5, 6, 7, 8}},
220+
{input: "C0", ptr: new([]uint), value: []uint{}},
221+
{input: "C80102030405060708", ptr: new([]uint), value: []uint{1, 2, 3, 4, 5, 6, 7, 8}},
222222

223223
// arrays
224-
{input: "C0", ptr: new([5]int), value: [5]int{}},
225-
{input: "C50102030405", ptr: new([5]int), value: [5]int{1, 2, 3, 4, 5}},
226-
{input: "C6010203040506", ptr: new([5]int), error: "rlp: input list has too many elements for [5]int"},
224+
{input: "C0", ptr: new([5]uint), value: [5]uint{}},
225+
{input: "C50102030405", ptr: new([5]uint), value: [5]uint{1, 2, 3, 4, 5}},
226+
{input: "C6010203040506", ptr: new([5]uint), error: "rlp: input list has too many elements for [5]uint"},
227227

228228
// byte slices
229229
{input: "01", ptr: new([]byte), value: []byte{1}},
@@ -280,17 +280,17 @@ var decodeTests = []decodeTest{
280280
},
281281

282282
// pointers
283-
{input: "00", ptr: new(*int), value: (*int)(nil)},
284-
{input: "80", ptr: new(*int), value: (*int)(nil)},
285-
{input: "C0", ptr: new(*int), value: (*int)(nil)},
286-
{input: "07", ptr: new(*int), value: intp(7)},
287-
{input: "8108", ptr: new(*int), value: intp(8)},
288-
{input: "C109", ptr: new(*[]int), value: &[]int{9}},
283+
{input: "00", ptr: new(*uint), value: (*uint)(nil)},
284+
{input: "80", ptr: new(*uint), value: (*uint)(nil)},
285+
{input: "C0", ptr: new(*uint), value: (*uint)(nil)},
286+
{input: "07", ptr: new(*uint), value: uintp(7)},
287+
{input: "8108", ptr: new(*uint), value: uintp(8)},
288+
{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
289289
{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
290290

291291
// pointer should be reset to nil
292-
{input: "05", ptr: sharedPtr, value: intp(5)},
293-
{input: "80", ptr: sharedPtr, value: (*int)(nil)},
292+
{input: "05", ptr: sharedPtr, value: uintp(5)},
293+
{input: "80", ptr: sharedPtr, value: (*uint)(nil)},
294294

295295
// interface{}
296296
{input: "00", ptr: new(interface{}), value: []byte{0}},
@@ -301,7 +301,7 @@ var decodeTests = []decodeTest{
301301
{input: "C50183040404", ptr: new(interface{}), value: []interface{}{[]byte{1}, []byte{4, 4, 4}}},
302302
}
303303

304-
func intp(i int) *int { return &i }
304+
func uintp(i uint) *uint { return &i }
305305

306306
func runTests(t *testing.T, decode func([]byte, interface{}) error) {
307307
for i, test := range decodeTests {
@@ -434,8 +434,8 @@ func ExampleDecode() {
434434
input, _ := hex.DecodeString("C90A1486666F6F626172")
435435

436436
type example struct {
437-
A, B int
438-
private int // private fields are ignored
437+
A, B uint
438+
private uint // private fields are ignored
439439
String string
440440
}
441441

@@ -447,7 +447,7 @@ func ExampleDecode() {
447447
fmt.Printf("Decoded value: %#v\n", s)
448448
}
449449
// Output:
450-
// Decoded value: rlp.example{A:10, B:20, private:0, String:"foobar"}
450+
// Decoded value: rlp.example{A:0xa, B:0x14, private:0x0, String:"foobar"}
451451
}
452452

453453
func ExampleStream() {

‎rlp/typecache.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ func genTypeInfo(typ reflect.Type) (info *typeinfo, err error) {
5757
return info, nil
5858
}
5959

60-
func isInteger(k reflect.Kind) bool {
61-
return k >= reflect.Int && k <= reflect.Uintptr
60+
func isUint(k reflect.Kind) bool {
61+
return k >= reflect.Uint && k <= reflect.Uintptr
6262
}

0 commit comments

Comments
 (0)