@@ -239,7 +239,7 @@ where
239
239
// the intended recipient).
240
240
let value_msat = if cur_value_msat == 0 { hop. fee_msat } else { cur_value_msat } ;
241
241
let cltv = if cur_cltv == starting_htlc_offset {
242
- hop. cltv_expiry_delta + starting_htlc_offset
242
+ hop. cltv_expiry_delta . saturating_add ( starting_htlc_offset)
243
243
} else {
244
244
cur_cltv
245
245
} ;
@@ -307,7 +307,7 @@ where
307
307
if cur_value_msat >= 21000000 * 100000000 * 1000 {
308
308
return Err ( APIError :: InvalidRoute { err : "Channel fees overflowed?" . to_owned ( ) } ) ;
309
309
}
310
- cur_cltv += hop. cltv_expiry_delta as u32 ;
310
+ cur_cltv = cur_cltv . saturating_add ( hop. cltv_expiry_delta as u32 ) ;
311
311
if cur_cltv >= 500000000 {
312
312
return Err ( APIError :: InvalidRoute { err : "Channel CLTV overflowed?" . to_owned ( ) } ) ;
313
313
}
@@ -333,9 +333,12 @@ pub(crate) fn set_max_path_length(
333
333
. saturating_add ( PAYLOAD_HMAC_LEN ) ;
334
334
335
335
const OVERPAY_ESTIMATE_MULTIPLER : u64 = 3 ;
336
- let final_value_msat_with_overpay_buffer = core:: cmp:: max (
337
- route_params. final_value_msat . saturating_mul ( OVERPAY_ESTIMATE_MULTIPLER ) ,
338
- MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY ,
336
+ let final_value_msat_with_overpay_buffer = core:: cmp:: min (
337
+ 0x1000_0000 ,
338
+ core:: cmp:: max (
339
+ route_params. final_value_msat . saturating_mul ( OVERPAY_ESTIMATE_MULTIPLER ) ,
340
+ MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY ,
341
+ )
339
342
) ;
340
343
341
344
let blinded_tail_opt = route_params
@@ -357,7 +360,7 @@ pub(crate) fn set_max_path_length(
357
360
short_channel_id : 42 ,
358
361
channel_features : ChannelFeatures :: empty ( ) ,
359
362
fee_msat : final_value_msat_with_overpay_buffer,
360
- cltv_expiry_delta : route_params. payment_params . max_total_cltv_expiry_delta ,
363
+ cltv_expiry_delta : core :: cmp :: min ( route_params. payment_params . max_total_cltv_expiry_delta , 0x1000_0000 ) ,
361
364
maybe_announced_channel : false ,
362
365
} ;
363
366
let mut num_reserved_bytes: usize = 0 ;
@@ -1280,7 +1283,7 @@ fn decode_next_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(
1280
1283
mod tests {
1281
1284
use crate :: io;
1282
1285
use crate :: ln:: msgs;
1283
- use crate :: routing:: router:: { Path , Route , RouteHop } ;
1286
+ use crate :: routing:: router:: { PaymentParameters , Path , Route , RouteHop } ;
1284
1287
use crate :: types:: features:: { ChannelFeatures , NodeFeatures } ;
1285
1288
use crate :: types:: payment:: PaymentHash ;
1286
1289
use crate :: util:: ser:: { VecWriter , Writeable , Writer } ;
@@ -1292,7 +1295,7 @@ mod tests {
1292
1295
use bitcoin:: secp256k1:: Secp256k1 ;
1293
1296
use bitcoin:: secp256k1:: { PublicKey , SecretKey } ;
1294
1297
1295
- use super :: OnionKeys ;
1298
+ use super :: * ;
1296
1299
1297
1300
fn get_test_session_key ( ) -> SecretKey {
1298
1301
let hex = "4141414141414141414141414141414141414141414141414141414141414141" ;
@@ -1607,4 +1610,19 @@ mod tests {
1607
1610
writer. write_all ( & self . data [ ..] )
1608
1611
}
1609
1612
}
1613
+
1614
+ #[ test]
1615
+ fn max_length_with_no_cltv_limit ( ) {
1616
+ // While users generally shouldn't do this, we shouldn't overflow when
1617
+ // `max_total_cltv_expiry_delta` is `u32::MAX`.
1618
+ let recipient = PublicKey :: from_slice ( & [ 2 ; 33 ] ) . unwrap ( ) ;
1619
+ let mut route_params = RouteParameters {
1620
+ payment_params : PaymentParameters :: for_keysend ( recipient, u32:: MAX , true ) ,
1621
+ final_value_msat : u64:: MAX ,
1622
+ max_total_routing_fee_msat : Some ( u64:: MAX ) ,
1623
+ } ;
1624
+ route_params. payment_params . max_total_cltv_expiry_delta = u32:: MAX ;
1625
+ let recipient_onion = RecipientOnionFields :: spontaneous_empty ( ) ;
1626
+ set_max_path_length ( & mut route_params, & recipient_onion, None , None , 42 ) . unwrap ( ) ;
1627
+ }
1610
1628
}
0 commit comments