@@ -85,7 +85,8 @@ macro_rules! _check_encoded_tlv_order {
85
85
( $last_type: expr, $type: expr, ( static_value, $value: expr) ) => { } ;
86
86
( $last_type: expr, $type: expr, $fieldty: tt) => {
87
87
if let Some ( t) = $last_type {
88
- #[ allow( unused_comparisons) ] // Note that $type may be 0 making the following comparison always false
88
+ // Note that $type may be 0 making the following comparison always false
89
+ #[ allow( unused_comparisons) ]
89
90
( debug_assert!( t < $type) )
90
91
}
91
92
$last_type = Some ( $type) ;
@@ -196,7 +197,8 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
196
197
$len. 0 += field_len;
197
198
} ;
198
199
( $len: expr, $type: expr, $field: expr, required_vec) => {
199
- $crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, $crate:: util:: ser:: WithoutLength ( & $field) , required) ;
200
+ let field = $crate:: util:: ser:: WithoutLength ( & $field) ;
201
+ $crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, field, required) ;
200
202
} ;
201
203
( $len: expr, $optional_type: expr, $optional_field: expr, option) => {
202
204
if let Some ( ref field) = $optional_field {
@@ -215,7 +217,8 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
215
217
$crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, $field, option) ;
216
218
} ;
217
219
( $len: expr, $type: expr, $field: expr, ( option, encoding: ( $fieldty: ty, $encoding: ident) ) ) => {
218
- $crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, $field. map( |f| $encoding( f) ) , option) ;
220
+ let field = $field. map( |f| $encoding( f) ) ;
221
+ $crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, field, option) ;
219
222
} ;
220
223
( $len: expr, $type: expr, $field: expr, upgradable_required) => {
221
224
$crate:: _get_varint_length_prefixed_tlv_length!( $len, $type, $field, required) ;
@@ -260,7 +263,8 @@ macro_rules! _encode_varint_length_prefixed_tlv {
260
263
#[ macro_export]
261
264
macro_rules! _check_decoded_tlv_order {
262
265
( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( default_value, $default: expr) ) => { {
263
- #[ allow( unused_comparisons) ] // Note that $type may be 0 making the second comparison always false
266
+ // Note that $type may be 0 making the second comparison always false
267
+ #[ allow( unused_comparisons) ]
264
268
let invalid_order = ( $last_seen_type. is_none( ) || $last_seen_type. unwrap( ) < $type) && $typ. 0 > $type;
265
269
if invalid_order {
266
270
$field = $default. into( ) ;
@@ -269,7 +273,8 @@ macro_rules! _check_decoded_tlv_order {
269
273
( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ( static_value, $value: expr) ) => {
270
274
} ;
271
275
( $last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => { {
272
- #[ allow( unused_comparisons) ] // Note that $type may be 0 making the second comparison always false
276
+ // Note that $type may be 0 making the second comparison always false
277
+ #[ allow( unused_comparisons) ]
273
278
let invalid_order = ( $last_seen_type. is_none( ) || $last_seen_type. unwrap( ) < $type) && $typ. 0 > $type;
274
279
if invalid_order {
275
280
return Err ( DecodeError :: InvalidValue ) ;
@@ -313,7 +318,8 @@ macro_rules! _check_decoded_tlv_order {
313
318
#[ macro_export]
314
319
macro_rules! _check_missing_tlv {
315
320
( $last_seen_type: expr, $type: expr, $field: ident, ( default_value, $default: expr) ) => { {
316
- #[ allow( unused_comparisons) ] // Note that $type may be 0 making the second comparison always false
321
+ // Note that $type may be 0 making the second comparison always false
322
+ #[ allow( unused_comparisons) ]
317
323
let missing_req_type = $last_seen_type. is_none( ) || $last_seen_type. unwrap( ) < $type;
318
324
if missing_req_type {
319
325
$field = $default. into( ) ;
@@ -323,7 +329,8 @@ macro_rules! _check_missing_tlv {
323
329
$field = $value;
324
330
} ;
325
331
( $last_seen_type: expr, $type: expr, $field: ident, required) => { {
326
- #[ allow( unused_comparisons) ] // Note that $type may be 0 making the second comparison always false
332
+ // Note that $type may be 0 making the second comparison always false
333
+ #[ allow( unused_comparisons) ]
327
334
let missing_req_type = $last_seen_type. is_none( ) || $last_seen_type. unwrap( ) < $type;
328
335
if missing_req_type {
329
336
return Err ( DecodeError :: InvalidValue ) ;
@@ -1339,53 +1346,59 @@ mod tests {
1339
1346
#[ test]
1340
1347
fn tlv_v_short_read ( ) {
1341
1348
// We only expect a u32 for type 3 (which we are given), but the L says its 8 bytes.
1342
- if let Err ( DecodeError :: ShortRead ) = tlv_reader ( & <Vec < u8 > >:: from_hex (
1343
- concat ! ( "0100" , "0208deadbeef1badbeef" , "0308deadbeef" )
1344
- ) . unwrap ( ) [ ..] ) {
1349
+ let buf = <Vec < u8 > >:: from_hex (
1350
+ concat ! ( "0100" , "0208deadbeef1badbeef" , "0308deadbeef" )
1351
+ ) . unwrap ( ) ;
1352
+ if let Err ( DecodeError :: ShortRead ) = tlv_reader ( & buf[ ..] ) {
1345
1353
} else { panic ! ( ) ; }
1346
1354
}
1347
1355
1348
1356
#[ test]
1349
1357
fn tlv_types_out_of_order ( ) {
1350
- if let Err ( DecodeError :: InvalidValue ) = tlv_reader ( & <Vec < u8 > >:: from_hex (
1351
- concat ! ( "0100" , "0304deadbeef" , "0208deadbeef1badbeef" )
1352
- ) . unwrap ( ) [ ..] ) {
1358
+ let buf = <Vec < u8 > >:: from_hex ( concat ! ( "0100" , "0304deadbeef" , "0208deadbeef1badbeef" ) ) . unwrap ( ) ;
1359
+ if let Err ( DecodeError :: InvalidValue ) = tlv_reader ( & buf[ ..] ) {
1353
1360
} else { panic ! ( ) ; }
1354
1361
// ...even if its some field we don't understand
1355
- if let Err ( DecodeError :: InvalidValue ) = tlv_reader ( & <Vec < u8 > >:: from_hex (
1356
- concat ! ( "0208deadbeef1badbeef" , "0100" , "0304deadbeef" )
1357
- ) . unwrap ( ) [ ..] ) {
1362
+ let buf = <Vec < u8 > >:: from_hex (
1363
+ concat ! ( "0208deadbeef1badbeef" , "0100" , "0304deadbeef" )
1364
+ ) . unwrap ( ) ;
1365
+ if let Err ( DecodeError :: InvalidValue ) = tlv_reader ( & buf[ ..] ) {
1358
1366
} else { panic ! ( ) ; }
1359
1367
}
1360
1368
1361
1369
#[ test]
1362
1370
fn tlv_req_type_missing_or_extra ( ) {
1363
1371
// It's also bad if they included even fields we don't understand
1364
- if let Err ( DecodeError :: UnknownRequiredFeature ) = tlv_reader ( & <Vec < u8 > >:: from_hex (
1372
+ let buf = <Vec < u8 > >:: from_hex (
1365
1373
concat ! ( "0100" , "0208deadbeef1badbeef" , "0304deadbeef" , "0600" )
1366
- ) . unwrap ( ) [ ..] ) {
1374
+ ) . unwrap ( ) ;
1375
+ if let Err ( DecodeError :: UnknownRequiredFeature ) = tlv_reader ( & buf[ ..] ) {
1367
1376
} else { panic ! ( ) ; }
1368
1377
// ... or if they're missing fields we need
1369
- if let Err ( DecodeError :: InvalidValue ) = tlv_reader ( & <Vec < u8 > >:: from_hex (
1370
- concat ! ( "0100" , "0208deadbeef1badbeef" )
1371
- ) . unwrap ( ) [ ..] ) {
1378
+ let buf = <Vec < u8 > >:: from_hex (
1379
+ concat ! ( "0100" , "0208deadbeef1badbeef" )
1380
+ ) . unwrap ( ) ;
1381
+ if let Err ( DecodeError :: InvalidValue ) = tlv_reader ( & buf[ ..] ) {
1372
1382
} else { panic ! ( ) ; }
1373
1383
// ... even if that field is even
1374
- if let Err ( DecodeError :: InvalidValue ) = tlv_reader ( & <Vec < u8 > >:: from_hex (
1384
+ let buf = <Vec < u8 > >:: from_hex (
1375
1385
concat ! ( "0304deadbeef" , "0500" )
1376
- ) . unwrap ( ) [ ..] ) {
1386
+ ) . unwrap ( ) ;
1387
+ if let Err ( DecodeError :: InvalidValue ) = tlv_reader ( & buf[ ..] ) {
1377
1388
} else { panic ! ( ) ; }
1378
1389
}
1379
1390
1380
1391
#[ test]
1381
1392
fn tlv_simple_good_cases ( ) {
1382
- assert_eq ! ( tlv_reader( & <Vec <u8 >>:: from_hex(
1383
- concat!( "0208deadbeef1badbeef" , "03041bad1dea" )
1384
- ) . unwrap( ) [ ..] ) . unwrap( ) ,
1393
+ let buf = <Vec < u8 > >:: from_hex (
1394
+ concat ! ( "0208deadbeef1badbeef" , "03041bad1dea" )
1395
+ ) . unwrap ( ) ;
1396
+ assert_eq ! ( tlv_reader( & buf[ ..] ) . unwrap( ) ,
1385
1397
( 0xdeadbeef1badbeef , 0x1bad1dea , None ) ) ;
1386
- assert_eq ! ( tlv_reader( & <Vec <u8 >>:: from_hex(
1387
- concat!( "0208deadbeef1badbeef" , "03041bad1dea" , "040401020304" )
1388
- ) . unwrap( ) [ ..] ) . unwrap( ) ,
1398
+ let buf = <Vec < u8 > >:: from_hex (
1399
+ concat ! ( "0208deadbeef1badbeef" , "03041bad1dea" , "040401020304" )
1400
+ ) . unwrap ( ) ;
1401
+ assert_eq ! ( tlv_reader( & buf[ ..] ) . unwrap( ) ,
1389
1402
( 0xdeadbeef1badbeef , 0x1bad1dea , Some ( 0x01020304 ) ) ) ;
1390
1403
}
1391
1404
@@ -1407,26 +1420,30 @@ mod tests {
1407
1420
1408
1421
#[ test]
1409
1422
fn upgradable_tlv_simple_good_cases ( ) {
1410
- assert_eq ! ( upgradable_tlv_reader( & <Vec <u8 >>:: from_hex(
1411
- concat!( "0204deadbeef" , "03041bad1dea" , "0404deadbeef" )
1412
- ) . unwrap( ) [ ..] ) . unwrap( ) ,
1423
+ let buf = <Vec < u8 > >:: from_hex (
1424
+ concat ! ( "0204deadbeef" , "03041bad1dea" , "0404deadbeef" )
1425
+ ) . unwrap ( ) ;
1426
+ assert_eq ! ( upgradable_tlv_reader( & buf[ ..] ) . unwrap( ) ,
1413
1427
Some ( TestUpgradable { a: 0xdeadbeef , b: 0x1bad1dea , c: Some ( 0xdeadbeef ) } ) ) ;
1414
1428
1415
- assert_eq ! ( upgradable_tlv_reader ( & <Vec <u8 >>:: from_hex(
1429
+ let buf = <Vec < u8 > >:: from_hex (
1416
1430
concat ! ( "0204deadbeef" , "03041bad1dea" )
1417
- ) . unwrap( ) [ ..] ) . unwrap( ) ,
1431
+ ) . unwrap ( ) ;
1432
+ assert_eq ! ( upgradable_tlv_reader( & buf[ ..] ) . unwrap( ) ,
1418
1433
Some ( TestUpgradable { a: 0xdeadbeef , b: 0x1bad1dea , c: None } ) ) ;
1419
1434
}
1420
1435
1421
1436
#[ test]
1422
1437
fn missing_required_upgradable ( ) {
1423
- if let Err ( DecodeError :: InvalidValue ) = upgradable_tlv_reader ( & <Vec < u8 > >:: from_hex (
1438
+ let buf = <Vec < u8 > >:: from_hex (
1424
1439
concat ! ( "0100" , "0204deadbeef" )
1425
- ) . unwrap ( ) [ ..] ) {
1440
+ ) . unwrap ( ) ;
1441
+ if let Err ( DecodeError :: InvalidValue ) = upgradable_tlv_reader ( & buf[ ..] ) {
1426
1442
} else { panic ! ( ) ; }
1427
- if let Err ( DecodeError :: InvalidValue ) = upgradable_tlv_reader ( & <Vec < u8 > >:: from_hex (
1443
+ let buf = <Vec < u8 > >:: from_hex (
1428
1444
concat ! ( "0100" , "03041bad1dea" )
1429
- ) . unwrap ( ) [ ..] ) {
1445
+ ) . unwrap ( ) ;
1446
+ if let Err ( DecodeError :: InvalidValue ) = upgradable_tlv_reader ( & buf[ ..] ) {
1430
1447
} else { panic ! ( ) ; }
1431
1448
}
1432
1449
0 commit comments