Skip to content

Commit

Permalink
psbt: Allocate correctly sized buffer for psbt_to_bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Griffiths <[email protected]>
  • Loading branch information
jgriffiths authored and rustyrussell committed Aug 7, 2020
1 parent fef155a commit 908a839
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions bitcoin/psbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 908a839

Please sign in to comment.