mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 17:47:30 +01:00
wire: add fromwire_tal_arrn() helper.
Does the allocation and copying; this is useful because we can avoid being fooled into doing giant allocations. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
524d22e4cc
commit
c92e782e22
5 changed files with 27 additions and 15 deletions
|
@ -12,8 +12,8 @@ struct onionreply *fromwire_onionreply(const tal_t *ctx,
|
||||||
const u8 **cursor, size_t *max)
|
const u8 **cursor, size_t *max)
|
||||||
{
|
{
|
||||||
struct onionreply *r = tal(ctx, struct onionreply);
|
struct onionreply *r = tal(ctx, struct onionreply);
|
||||||
r->contents = tal_arr(r, u8, fromwire_u16(cursor, max));
|
r->contents = fromwire_tal_arrn(r, cursor, max,
|
||||||
fromwire_u8_array(cursor, max, r->contents, tal_count(r->contents));
|
fromwire_u16(cursor, max));
|
||||||
if (!*cursor)
|
if (!*cursor)
|
||||||
return tal_free(r);
|
return tal_free(r);
|
||||||
return r;
|
return r;
|
||||||
|
|
|
@ -104,10 +104,9 @@ static void printwire_addresses(const u8 **cursor, size_t *plen, size_t len)
|
||||||
static void printwire_encoded_short_ids(const u8 **cursor, size_t *plen, size_t len)
|
static void printwire_encoded_short_ids(const u8 **cursor, size_t *plen, size_t len)
|
||||||
{
|
{
|
||||||
struct short_channel_id *scids;
|
struct short_channel_id *scids;
|
||||||
u8 *arr = tal_arr(tmpctx, u8, len);
|
u8 *arr = fromwire_tal_arrn(tmpctx, cursor, plen, len);
|
||||||
|
|
||||||
fromwire_u8_array(cursor, plen, arr, len);
|
if (!arr)
|
||||||
if (!*cursor)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
printf("[");
|
printf("[");
|
||||||
|
|
|
@ -21,8 +21,7 @@ struct gossip_getnodes_entry *fromwire_gossip_getnodes_entry(const tal_t *ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
flen = fromwire_u16(pptr, max);
|
flen = fromwire_u16(pptr, max);
|
||||||
entry->features = tal_arr(entry, u8, flen);
|
entry->features = fromwire_tal_arrn(entry, pptr, max, flen);
|
||||||
fromwire_u8_array(pptr, max, entry->features, flen);
|
|
||||||
|
|
||||||
numaddresses = fromwire_u8(pptr, max);
|
numaddresses = fromwire_u8(pptr, max);
|
||||||
|
|
||||||
|
|
|
@ -308,6 +308,19 @@ void fromwire_u8_array(const u8 **cursor, size_t *max, u8 *arr, size_t num)
|
||||||
fromwire(cursor, max, arr, num);
|
fromwire(cursor, max, arr, num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u8 *fromwire_tal_arrn(const tal_t *ctx,
|
||||||
|
const u8 **cursor, size_t *max, size_t num)
|
||||||
|
{
|
||||||
|
u8 *arr;
|
||||||
|
if (num > *max) {
|
||||||
|
fromwire_fail(cursor, max);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
arr = tal_arr(ctx, u8, num);
|
||||||
|
fromwire_u8_array(cursor, max, arr, num);
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
void fromwire_pad(const u8 **cursor, size_t *max, size_t num)
|
void fromwire_pad(const u8 **cursor, size_t *max, size_t num)
|
||||||
{
|
{
|
||||||
fromwire(cursor, max, NULL, num);
|
fromwire(cursor, max, NULL, num);
|
||||||
|
@ -393,20 +406,19 @@ struct bitcoin_tx_output *fromwire_bitcoin_tx_output(const tal_t *ctx,
|
||||||
struct bitcoin_tx_output *output = tal(ctx, struct bitcoin_tx_output);
|
struct bitcoin_tx_output *output = tal(ctx, struct bitcoin_tx_output);
|
||||||
output->amount = fromwire_amount_sat(cursor, max);
|
output->amount = fromwire_amount_sat(cursor, max);
|
||||||
u16 script_len = fromwire_u16(cursor, max);
|
u16 script_len = fromwire_u16(cursor, max);
|
||||||
output->script = tal_arr(output, u8, script_len);
|
output->script = fromwire_tal_arrn(output, cursor, max, script_len);
|
||||||
fromwire_u8_array(cursor, max, output->script, script_len);
|
if (!*cursor)
|
||||||
|
return tal_free(output);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct witscript *fromwire_witscript(const tal_t *ctx, const u8 **cursor, size_t *max)
|
struct witscript *fromwire_witscript(const tal_t *ctx, const u8 **cursor, size_t *max)
|
||||||
{
|
{
|
||||||
struct witscript *retval;
|
struct witscript *retval = tal(ctx, struct witscript);
|
||||||
u16 len = fromwire_u16(cursor, max);
|
u16 len = fromwire_u16(cursor, max);
|
||||||
if (!len)
|
retval->ptr = fromwire_tal_arrn(retval, cursor, max, len);
|
||||||
return NULL;
|
if (!*cursor)
|
||||||
retval = tal(ctx, struct witscript);
|
return tal_free(retval);
|
||||||
retval->ptr = tal_arr(retval, u8, len);
|
|
||||||
fromwire_u8_array(cursor, max, retval->ptr, len);
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -136,6 +136,8 @@ struct amount_sat fromwire_amount_sat(const u8 **cursor, size_t *max);
|
||||||
void fromwire_pad(const u8 **cursor, size_t *max, size_t num);
|
void fromwire_pad(const u8 **cursor, size_t *max, size_t num);
|
||||||
|
|
||||||
void fromwire_u8_array(const u8 **cursor, size_t *max, u8 *arr, size_t num);
|
void fromwire_u8_array(const u8 **cursor, size_t *max, u8 *arr, size_t num);
|
||||||
|
u8 *fromwire_tal_arrn(const tal_t *ctx,
|
||||||
|
const u8 **cursor, size_t *max, size_t num);
|
||||||
char *fromwire_wirestring(const tal_t *ctx, const u8 **cursor, size_t *max);
|
char *fromwire_wirestring(const tal_t *ctx, const u8 **cursor, size_t *max);
|
||||||
struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx,
|
struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx,
|
||||||
const u8 **cursor, size_t *max);
|
const u8 **cursor, size_t *max);
|
||||||
|
|
Loading…
Add table
Reference in a new issue