8
8
"fmt"
9
9
"io"
10
10
"net"
11
+ "runtime"
11
12
"sync"
12
13
"time"
13
14
)
@@ -371,12 +372,10 @@ func (f *framer) parseFrame() (frame frame, err error) {
371
372
372
373
defer func () {
373
374
if r := recover (); r != nil {
374
- if perr , ok := r .(error ); ok {
375
- err = perr
376
- return
375
+ if _ , ok := r .(runtime.Error ); ok {
376
+ panic (r )
377
377
}
378
-
379
- panic (r )
378
+ err = r .(error )
380
379
}
381
380
}()
382
381
@@ -1161,18 +1160,28 @@ func (f *framer) readByte() byte {
1161
1160
}
1162
1161
1163
1162
func (f * framer ) readInt () (n int ) {
1163
+ if len (f .rbuf ) < 4 {
1164
+ panic (fmt .Errorf ("not enough bytes in buffer to read int require 4 got: %d" , len (f .rbuf )))
1165
+ }
1166
+
1164
1167
n = int (int32 (f .rbuf [0 ])<< 24 | int32 (f .rbuf [1 ])<< 16 | int32 (f .rbuf [2 ])<< 8 | int32 (f .rbuf [3 ]))
1165
1168
f .rbuf = f .rbuf [4 :]
1166
1169
return
1167
1170
}
1168
1171
1169
1172
func (f * framer ) readShort () (n uint16 ) {
1173
+ if len (f .rbuf ) < 2 {
1174
+ panic (fmt .Errorf ("not enough bytes in buffer to read short require 2 got: %d" , len (f .rbuf )))
1175
+ }
1170
1176
n = uint16 (f .rbuf [0 ])<< 8 | uint16 (f .rbuf [1 ])
1171
1177
f .rbuf = f .rbuf [2 :]
1172
1178
return
1173
1179
}
1174
1180
1175
1181
func (f * framer ) readLong () (n int64 ) {
1182
+ if len (f .rbuf ) < 8 {
1183
+ panic (fmt .Errorf ("not enough bytes in buffer to read long require 8 got: %d" , len (f .rbuf )))
1184
+ }
1176
1185
n = int64 (f .rbuf [0 ])<< 56 | int64 (f .rbuf [1 ])<< 48 | int64 (f .rbuf [2 ])<< 40 | int64 (f .rbuf [3 ])<< 32 |
1177
1186
int64 (f .rbuf [4 ])<< 24 | int64 (f .rbuf [5 ])<< 16 | int64 (f .rbuf [6 ])<< 8 | int64 (f .rbuf [7 ])
1178
1187
f .rbuf = f .rbuf [8 :]
@@ -1181,19 +1190,33 @@ func (f *framer) readLong() (n int64) {
1181
1190
1182
1191
func (f * framer ) readString () (s string ) {
1183
1192
size := f .readShort ()
1193
+
1194
+ if len (f .rbuf ) < int (size ) {
1195
+ panic (fmt .Errorf ("not enough bytes in buffer to read string require %d got: %d" , size , len (f .rbuf )))
1196
+ }
1197
+
1184
1198
s = string (f .rbuf [:size ])
1185
1199
f .rbuf = f .rbuf [size :]
1186
1200
return
1187
1201
}
1188
1202
1189
1203
func (f * framer ) readLongString () (s string ) {
1190
1204
size := f .readInt ()
1205
+
1206
+ if len (f .rbuf ) < size {
1207
+ panic (fmt .Errorf ("not enough bytes in buffer to read long string require %d got: %d" , size , len (f .rbuf )))
1208
+ }
1209
+
1191
1210
s = string (f .rbuf [:size ])
1192
1211
f .rbuf = f .rbuf [size :]
1193
1212
return
1194
1213
}
1195
1214
1196
1215
func (f * framer ) readUUID () * UUID {
1216
+ if len (f .rbuf ) < 16 {
1217
+ panic (fmt .Errorf ("not enough bytes in buffer to read uuid require %d got: %d" , 16 , len (f .rbuf )))
1218
+ }
1219
+
1197
1220
// TODO: how to handle this error, if it is a uuid, then sureley, problems?
1198
1221
u , _ := UUIDFromBytes (f .rbuf [:16 ])
1199
1222
f .rbuf = f .rbuf [16 :]
@@ -1217,6 +1240,10 @@ func (f *framer) readBytes() []byte {
1217
1240
return nil
1218
1241
}
1219
1242
1243
+ if len (f .rbuf ) < size {
1244
+ panic (fmt .Errorf ("not enough bytes in buffer to read bytes require %d got: %d" , size , len (f .rbuf )))
1245
+ }
1246
+
1220
1247
// we cant make assumptions about the length of the life of the supplied byte
1221
1248
// slice so we defensivly copy it out of the underlying buffer. This has the
1222
1249
// downside of increasing allocs per read but will provide much greater memory
@@ -1240,11 +1267,19 @@ func (f *framer) readShortBytes() []byte {
1240
1267
}
1241
1268
1242
1269
func (f * framer ) readInet () (net.IP , int ) {
1270
+ if len (f .rbuf ) < 1 {
1271
+ panic (fmt .Errorf ("not enough bytes in buffer to read inet size require %d got: %d" , 1 , len (f .rbuf )))
1272
+ }
1273
+
1243
1274
size := f .rbuf [0 ]
1244
1275
f .rbuf = f .rbuf [1 :]
1245
1276
1246
1277
if ! (size == 4 || size == 16 ) {
1247
- panic (fmt .Sprintf ("invalid IP size: %d" , size ))
1278
+ panic (fmt .Errorf ("invalid IP size: %d" , size ))
1279
+ }
1280
+
1281
+ if len (f .rbuf ) < 1 {
1282
+ panic (fmt .Errorf ("not enough bytes in buffer to read inet require %d got: %d" , size , len (f .rbuf )))
1248
1283
}
1249
1284
1250
1285
ip := make ([]byte , size )
0 commit comments