From 908a8399e26263034f6cf89b2addbef875430122 Mon Sep 17 00:00:00 2001 From: Jon Griffiths Date: Thu, 30 Jul 2020 15:30:39 +1200 Subject: [PATCH] psbt: Allocate correctly sized buffer for psbt_to_bytes Signed-off-by: Jon Griffiths --- bitcoin/psbt.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/bitcoin/psbt.c b/bitcoin/psbt.c index fdf8be49c..ba8f74b4e 100644 --- a/bitcoin/psbt.c +++ b/bitcoin/psbt.c @@ -385,17 +385,18 @@ REGISTER_TYPE_TO_STRING(wally_psbt, psbt_to_b64); const u8 *psbt_get_bytes(const tal_t *ctx, const struct wally_psbt *psbt, size_t *bytes_written) { - /* the libwally API doesn't do anything helpful for allocating - * things here -- to compensate we do a single shot large alloc - */ - size_t room = 1024 * 1000; - u8 *pbt_bytes = tal_arr(ctx, u8, room); - if (wally_psbt_to_bytes(psbt, 0, pbt_bytes, room, bytes_written) != WALLY_OK) { + size_t len = 0; + u8 *bytes; + + wally_psbt_get_length(psbt, 0, &len); + bytes = tal_arr(ctx, u8, len); + + if (wally_psbt_to_bytes(psbt, 0, bytes, len, bytes_written) != WALLY_OK || + *bytes_written != len) { /* something went wrong. bad libwally ?? */ abort(); } - tal_resize(&pbt_bytes, *bytes_written); - return pbt_bytes; + return bytes; } struct wally_psbt *psbt_from_bytes(const tal_t *ctx, const u8 *bytes,