psbt: Allocate correctly sized buffer for psbt_to_bytes

Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com>
This commit is contained in:
Jon Griffiths 2020-07-30 15:30:39 +12:00 committed by Rusty Russell
parent fef155a9e2
commit 908a8399e2
1 changed files with 9 additions and 8 deletions

View File

@ -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,