@@ -49,8 +49,15 @@ var BigEndian bigEndian
49
49
type littleEndian struct {}
50
50
51
51
func (littleEndian ) Uint16 (b []byte ) uint16 {
52
- _ = b [1 ] // bounds check hint to compiler; see golang.org/issue/14808
53
- return uint16 (b [0 ]) | uint16 (b [1 ])<< 8
52
+ // The previous implementation wouldn't allow an empty slice
53
+ switch len (b ) {
54
+ case 0 :
55
+ return 0
56
+ case 1 :
57
+ return uint16 (b [0 ])
58
+ default :
59
+ return uint16 (b [0 ]) | uint16 (b [1 ])<< 8
60
+ }
54
61
}
55
62
56
63
func (littleEndian ) PutUint16 (b []byte , v uint16 ) {
@@ -60,8 +67,19 @@ func (littleEndian) PutUint16(b []byte, v uint16) {
60
67
}
61
68
62
69
func (littleEndian ) Uint32 (b []byte ) uint32 {
63
- _ = b [3 ] // bounds check hint to compiler; see golang.org/issue/14808
64
- return uint32 (b [0 ]) | uint32 (b [1 ])<< 8 | uint32 (b [2 ])<< 16 | uint32 (b [3 ])<< 24
70
+ // The previous implementation wouldn't allow for example a 3 byte slice
71
+ switch len (b ) {
72
+ case 0 :
73
+ return 0
74
+ case 1 :
75
+ return uint32 (b [0 ])
76
+ case 2 :
77
+ return uint32 (b [0 ]) | uint32 (b [1 ])<< 8
78
+ case 3 :
79
+ return uint32 (b [0 ]) | uint32 (b [1 ])<< 8 | uint32 (b [2 ])<< 16
80
+ default :
81
+ return uint32 (b [0 ]) | uint32 (b [1 ])<< 8 | uint32 (b [2 ])<< 16 | uint32 (b [3 ])<< 24
82
+ }
65
83
}
66
84
67
85
func (littleEndian ) PutUint32 (b []byte , v uint32 ) {
@@ -73,9 +91,30 @@ func (littleEndian) PutUint32(b []byte, v uint32) {
73
91
}
74
92
75
93
func (littleEndian ) Uint64 (b []byte ) uint64 {
76
- _ = b [7 ] // bounds check hint to compiler; see golang.org/issue/14808
77
- return uint64 (b [0 ]) | uint64 (b [1 ])<< 8 | uint64 (b [2 ])<< 16 | uint64 (b [3 ])<< 24 |
78
- uint64 (b [4 ])<< 32 | uint64 (b [5 ])<< 40 | uint64 (b [6 ])<< 48 | uint64 (b [7 ])<< 56
94
+ switch len (b ) {
95
+ case 0 :
96
+ return 0
97
+ case 1 :
98
+ return uint64 (b [0 ])
99
+ case 2 :
100
+ return uint64 (b [0 ]) | uint64 (b [1 ])<< 8
101
+ case 3 :
102
+ return uint64 (b [0 ]) | uint64 (b [1 ])<< 8 | uint64 (b [2 ])<< 16
103
+ case 4 :
104
+ return uint64 (b [0 ]) | uint64 (b [1 ])<< 8 | uint64 (b [2 ])<< 16 | uint64 (b [3 ])<< 24
105
+ case 5 :
106
+ return uint64 (b [0 ]) | uint64 (b [1 ])<< 8 | uint64 (b [2 ])<< 16 | uint64 (b [3 ])<< 24 |
107
+ uint64 (b [4 ])<< 32
108
+ case 6 :
109
+ return uint64 (b [0 ]) | uint64 (b [1 ])<< 8 | uint64 (b [2 ])<< 16 | uint64 (b [3 ])<< 24 |
110
+ uint64 (b [4 ])<< 32 | uint64 (b [5 ])<< 40
111
+ case 7 :
112
+ return uint64 (b [0 ]) | uint64 (b [1 ])<< 8 | uint64 (b [2 ])<< 16 | uint64 (b [3 ])<< 24 |
113
+ uint64 (b [4 ])<< 32 | uint64 (b [5 ])<< 40 | uint64 (b [6 ])<< 48
114
+ default :
115
+ return uint64 (b [0 ]) | uint64 (b [1 ])<< 8 | uint64 (b [2 ])<< 16 | uint64 (b [3 ])<< 24 |
116
+ uint64 (b [4 ])<< 32 | uint64 (b [5 ])<< 40 | uint64 (b [6 ])<< 48 | uint64 (b [7 ])<< 56
117
+ }
79
118
}
80
119
81
120
func (littleEndian ) PutUint64 (b []byte , v uint64 ) {
0 commit comments