wiregen: Passing ctx to array helpers that require it

Some of the struct array helpers need to allocate data when
deserializing their fields. The `getnodes` reply is one such example
that allocates the hostname. Since the change to calling array helpers
the getnodes call was broken because it was attempting to allocate off
of the entry, which did not have a tal header, thus failing.
This commit is contained in:
Christian Decker 2017-03-18 15:50:56 +01:00 committed by Rusty Russell
parent d2e19c3735
commit b2ea4cfd66
3 changed files with 11 additions and 5 deletions

View File

@ -1,12 +1,12 @@
#include <lightningd/gossip_msg.h>
#include <wire/wire.h>
void fromwire_gossip_getnodes_entry(const u8 **pptr, size_t *max, struct gossip_getnodes_entry *entry)
void fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, size_t *max, struct gossip_getnodes_entry *entry)
{
u8 hostnamelen;
fromwire_pubkey(pptr, max, &entry->nodeid);
hostnamelen = fromwire_u8(pptr, max);
entry->hostname = tal_arr(entry, char, hostnamelen);
entry->hostname = tal_arr(ctx, char, hostnamelen);
fromwire_u8_array(pptr, max, (u8*)entry->hostname, hostnamelen);
entry->port = fromwire_u16(pptr, max);
}

View File

@ -9,7 +9,7 @@ struct gossip_getnodes_entry {
u16 port;
};
void fromwire_gossip_getnodes_entry(const u8 **pptr, size_t *max, struct gossip_getnodes_entry *entry);
void fromwire_gossip_getnodes_entry(const tal_t *ctx, const u8 **pptr, size_t *max, struct gossip_getnodes_entry *entry);
void towire_gossip_getnodes_entry(u8 **pptr, const struct gossip_getnodes_entry *entry);
#endif /* LIGHTNING_LIGHTGNINGD_GOSSIP_MSG_H */

View File

@ -23,6 +23,11 @@ type2size = {
'bool': 1
}
# These struct array helpers require a context to allocate from.
varlen_structs = [
'gossip_getnodes_entry',
]
class FieldType(object):
def __init__(self,name):
self.name = name
@ -235,8 +240,9 @@ class Message(object):
subcalls.append('\t\t{}[i] = fromwire_{}(&cursor, plen);'
.format(name, basetype))
else:
subcalls.append('\t\tfromwire_{}(&cursor, plen, {} + i);'
.format(basetype, name))
ctx = "ctx, " if basetype in varlen_structs else ""
subcalls.append('\t\tfromwire_{}({}&cursor, plen, {} + i);'
.format(basetype, ctx, name))
def print_fromwire(self,is_header):
ctx_arg = 'const tal_t *ctx, ' if self.has_variable_fields else ''