From b2ea4cfd660ce3b38049db5c7dd940088146a52b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 18 Mar 2017 15:50:56 +0100 Subject: [PATCH] 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. --- lightningd/gossip_msg.c | 4 ++-- lightningd/gossip_msg.h | 2 +- tools/generate-wire.py | 10 ++++++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lightningd/gossip_msg.c b/lightningd/gossip_msg.c index 4d3698851..3722b844f 100644 --- a/lightningd/gossip_msg.c +++ b/lightningd/gossip_msg.c @@ -1,12 +1,12 @@ #include #include -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); } diff --git a/lightningd/gossip_msg.h b/lightningd/gossip_msg.h index 3a543f297..40e7843d6 100644 --- a/lightningd/gossip_msg.h +++ b/lightningd/gossip_msg.h @@ -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 */ diff --git a/tools/generate-wire.py b/tools/generate-wire.py index f92961dc1..2d7cbbec1 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -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 ''