From 30c57f9d35bc2b074f898c8a48cf3dc1be1ea5b4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sun, 11 Jul 2021 16:29:42 +0930 Subject: [PATCH] lease_rates, funder: use overflow helpers Signed-off-by: Rusty Russell --- common/lease_rates.c | 9 ++--- plugins/funder.c | 80 ++++++++++++++++---------------------------- 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/common/lease_rates.c b/common/lease_rates.c index 4ab08c9d2..118269ad5 100644 --- a/common/lease_rates.c +++ b/common/lease_rates.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -102,15 +103,15 @@ bool lease_rates_calc_fee(struct lease_rates *rates, bool lease_rates_set_chan_fee_base_msat(struct lease_rates *rates, struct amount_msat amt) { - rates->channel_fee_max_base_msat = amt.millisatoshis; /* Raw: conversion */ - return rates->channel_fee_max_base_msat == amt.millisatoshis; /* Raw: comparsion */ + return assign_overflow_u32(&rates->channel_fee_max_base_msat, + amt.millisatoshis); /* Raw: conversion */ } bool lease_rates_set_lease_fee_sat(struct lease_rates *rates, struct amount_sat amt) { - rates->lease_fee_base_sat = amt.satoshis; /* Raw: conversion */ - return rates->lease_fee_base_sat == amt.satoshis; /* Raw: comparsion */ + return assign_overflow_u32(&rates->lease_fee_base_sat, + amt.satoshis); /* Raw: conversion */ } char *lease_rates_tohex(const tal_t *ctx, const struct lease_rates *rates) diff --git a/plugins/funder.c b/plugins/funder.c index 9dbbba5a4..fed510d3e 100644 --- a/plugins/funder.c +++ b/plugins/funder.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -832,7 +833,7 @@ parse_lease_rates(struct command *cmd, const char *buffer, u32 *lease_fee_basis, struct amount_sat *lease_fee_sats, u32 *funding_weight, - u32 *chan_fee_ppt, + u32 *channel_fee_max_proportional_thousandths, struct amount_msat *chan_fee_msats) { @@ -843,59 +844,37 @@ parse_lease_rates(struct command *cmd, const char *buffer, else if (lease_fee_basis || lease_fee_sats || funding_weight - || chan_fee_ppt + || channel_fee_max_proportional_thousandths || chan_fee_msats) policy->rates = default_lease_rates(policy); else policy->rates = NULL; + /* Sometimes a local macro is neater than the alternative */ +#define ASSIGN_OR_RETURN_FAIL(type, member) \ + do { \ + if (member && \ + !assign_overflow_##type(&policy->rates->member, *member)) \ + return command_fail_badparam(cmd, #member, \ + buffer, tok, "overflow"); \ +} while(0) - if (lease_fee_basis) { - policy->rates->lease_fee_basis = *lease_fee_basis; + ASSIGN_OR_RETURN_FAIL(u16, lease_fee_basis); + ASSIGN_OR_RETURN_FAIL(u16, funding_weight); + ASSIGN_OR_RETURN_FAIL(u16, channel_fee_max_proportional_thousandths); +#undef ASSIGN_OR_RETURN_FAIL - /* Check for overflow */ - if (policy->rates->lease_fee_basis != *lease_fee_basis) - return command_fail_badparam(cmd, "lease_fee_basis", - buffer, tok, "overflow"); + if (chan_fee_msats + && !assign_overflow_u32(&policy->rates->channel_fee_max_base_msat, + chan_fee_msats->millisatoshis /* Raw: conversion */)) { + return command_fail_badparam(cmd, "channel_fee_max_base_msat", + buffer, tok, "overflow"); } - - if (lease_fee_sats) { - policy->rates->lease_fee_base_sat - = lease_fee_sats->satoshis; /* Raw: conversion */ - if (policy->rates->lease_fee_base_sat - != lease_fee_sats->satoshis) /* Raw: comparison */ - return command_fail_badparam(cmd, "lease_fee_base_msat", - buffer, tok, "overflow"); - } - - if (funding_weight) { - policy->rates->funding_weight = *funding_weight; - - /* Check for overflow */ - if (policy->rates->funding_weight != *funding_weight) - return command_fail_badparam(cmd, "funding_weight", - buffer, tok, "overflow"); - } - - if (chan_fee_ppt) { - policy->rates->channel_fee_max_proportional_thousandths - = *chan_fee_ppt; - - /* Check for overflow */ - if (policy->rates->channel_fee_max_proportional_thousandths - != *chan_fee_ppt) - return command_fail_badparam(cmd, "channel_fee_max_proportional_thousandths", - buffer, tok, "overflow"); - } - - if (chan_fee_msats) { - policy->rates->channel_fee_max_base_msat - = chan_fee_msats->millisatoshis; /* Raw: conversion */ - if (policy->rates->channel_fee_max_base_msat - != chan_fee_msats->millisatoshis) /* Raw: comparison */ - return command_fail_badparam(cmd, - "channel_fee_max_base_msat", - buffer, tok, "overflow"); + if (lease_fee_sats + && !assign_overflow_u32(&policy->rates->lease_fee_base_sat, + lease_fee_sats->satoshis /* Raw: conversion */)) { + return command_fail_badparam(cmd, "lease_fee_base_sat", + buffer, tok, "overflow"); } return NULL; @@ -1126,9 +1105,8 @@ static char *option_channel_base(const char *arg, struct funder_policy *policy) if (!policy->rates) policy->rates = default_lease_rates(policy); - policy->rates->channel_fee_max_base_msat = amt.millisatoshis; /* Raw: conversion */ - - if (policy->rates->channel_fee_max_base_msat != amt.millisatoshis) /* Raw: comparison */ + if (!assign_overflow_u32(&policy->rates->channel_fee_max_base_msat, + amt.millisatoshis)) /* Raw: conversion */ return tal_fmt(NULL, "channel_fee_max_base_msat overflowed"); return NULL; @@ -1163,8 +1141,8 @@ static char *option_lease_fee_base(const char *arg, if (err) return err; - policy->rates->lease_fee_base_sat = amt.satoshis; /* Raw: conversion */ - if (policy->rates->lease_fee_base_sat != amt.satoshis) /* Raw: comparison */ + if (!assign_overflow_u32(&policy->rates->lease_fee_base_sat, + amt.satoshis)) /* Raw: conversion */ return tal_fmt(NULL, "lease_fee_base_sat overflowed"); return NULL;