Fix overflow in lightning-invoice amount_pico_btc.
authorValentine Wallace <vwallace@protonmail.com>
Tue, 30 Apr 2024 14:05:35 +0000 (10:05 -0400)
committerValentine Wallace <vwallace@protonmail.com>
Tue, 30 Apr 2024 14:05:35 +0000 (10:05 -0400)
lightning-invoice/src/lib.rs

index 20eaf7a82d27a7970872883536f7edce1da55c69..f06eeaa1744bfcf16e5c74d6e61423d8fc44d082 100644 (file)
@@ -1068,9 +1068,10 @@ impl RawBolt11Invoice {
                find_all_extract!(self.known_tagged_fields(), TaggedField::PrivateRoute(ref x), x).collect()
        }
 
+       /// Returns `None` if no amount is set or on overflow.
        pub fn amount_pico_btc(&self) -> Option<u64> {
-               self.hrp.raw_amount.map(|v| {
-                       v * self.hrp.si_prefix.as_ref().map_or(1_000_000_000_000, |si| { si.multiplier() })
+               self.hrp.raw_amount.and_then(|v| {
+                       v.checked_mul(self.hrp.si_prefix.as_ref().map_or(1_000_000_000_000, |si| { si.multiplier() }))
                })
        }