From ecb49d1a52f975d6150e97a19454bf38e0313675 Mon Sep 17 00:00:00 2001 From: darosior Date: Mon, 30 Mar 2020 15:22:37 +0200 Subject: [PATCH] common/amount: accept 0msat in parse_amount_sat() Changelog-fixed: bcli now handles 0msat outputs in gettxout. Co-authored-by: Rusty Russell --- common/amount.c | 7 ++++++- common/test/run-amount.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/common/amount.c b/common/amount.c index 7bb2e6d40096..71eee872c66e 100644 --- a/common/amount.c +++ b/common/amount.c @@ -181,6 +181,7 @@ bool parse_amount_msat(struct amount_msat *msat, const char *s, size_t slen) * [0-9]+ => satoshi. * [0-9]+sat => satoshi. * [0-9]+000msat => satoshi. + * 0msat => 0 satoshi * [0-9]+.[0-9]{1,8}btc => satoshi. */ bool parse_amount_sat(struct amount_sat *sat, const char *s, size_t slen) @@ -198,8 +199,12 @@ bool parse_amount_sat(struct amount_sat *sat, const char *s, size_t slen) if (!post_decimal_ptr && memeqstr(suffix_ptr, suffix_len, "sat")) return from_number(&sat->satoshis, s, whole_number_len, 0); if (!post_decimal_ptr && memeqstr(suffix_ptr, suffix_len, "msat")) { - if (!memends(s, whole_number_len, "000", strlen("000"))) + if (!memends(s, whole_number_len, "000", strlen("000"))) { + if (memeqstr(s, whole_number_len, "0")) + return from_number(&sat->satoshis, s, + whole_number_len, 0); return false; + } return from_number(&sat->satoshis, s, whole_number_len - 3, 0); } if (post_decimal_ptr && memeqstr(suffix_ptr, suffix_len, "btc")) diff --git a/common/test/run-amount.c b/common/test/run-amount.c index 6b510277f166..3b3e5596d65a 100644 --- a/common/test/run-amount.c +++ b/common/test/run-amount.c @@ -101,7 +101,7 @@ int main(void) PASS_SAT(&sat, "1000msat", 1); PASS_SAT(&sat, "1000000msat", 1000); PASS_SAT(&sat, "2100000000000000000msat", 2100000000000000ULL); - FAIL_SAT(&sat, "0msat"); + PASS_SAT(&sat, "0msat", 0); FAIL_SAT(&sat, "100msat"); FAIL_SAT(&sat, "2000000000000000999msat"); FAIL_SAT(&sat, "-1000msat");