gossipd: use array[32] not pointer for alias.

And use ARRAY_SIZE() everywhere which will break compile if it's not a
literal array, plus assertions that it's the same length.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-09-24 11:11:39 +09:30
committed by Christian Decker
parent 6c54a22d63
commit afc92dd757
6 changed files with 20 additions and 19 deletions

View File

@@ -202,7 +202,10 @@ static void json_getnodes_reply(struct subd *gossip UNUSED, const u8 *reply,
json_object_end(response);
continue;
}
esc = json_escape(NULL, (const char *)nodes[i]->alias);
esc = json_escape(NULL,
take(tal_strndup(NULL,
(const char *)nodes[i]->alias,
ARRAY_SIZE(nodes[i]->alias))));
json_add_escaped_string(response, "alias", take(esc));
json_add_hex(response, "color",
nodes[i]->color, ARRAY_SIZE(nodes[i]->color));

View File

@@ -1,3 +1,4 @@
#include <ccan/array_size/array_size.h>
#include <common/bolt11.h>
#include <common/wireaddr.h>
#include <lightningd/gossip_msg.h>
@@ -20,7 +21,6 @@ struct gossip_getnodes_entry *fromwire_gossip_getnodes_entry(const tal_t *ctx,
entry->last_timestamp = fromwire_u64(pptr, max);
if (entry->last_timestamp < 0) {
entry->addresses = NULL;
entry->alias = NULL;
return entry;
}
numaddresses = fromwire_u8(pptr, max);
@@ -33,10 +33,8 @@ struct gossip_getnodes_entry *fromwire_gossip_getnodes_entry(const tal_t *ctx,
return NULL;
}
}
/* Make sure alias is NUL terminated */
entry->alias = tal_arrz(entry, u8, fromwire_u8(pptr, max)+1);
fromwire(pptr, max, entry->alias, tal_count(entry->alias)-1);
fromwire(pptr, max, entry->color, sizeof(entry->color));
fromwire(pptr, max, entry->alias, ARRAY_SIZE(entry->alias));
fromwire(pptr, max, entry->color, ARRAY_SIZE(entry->color));
return entry;
}
@@ -58,9 +56,8 @@ void towire_gossip_getnodes_entry(u8 **pptr,
for (i=0; i<numaddresses; i++) {
towire_wireaddr(pptr, &entry->addresses[i]);
}
towire_u8(pptr, tal_count(entry->alias));
towire(pptr, entry->alias, tal_count(entry->alias));
towire(pptr, entry->color, sizeof(entry->color));
towire(pptr, entry->alias, ARRAY_SIZE(entry->alias));
towire(pptr, entry->color, ARRAY_SIZE(entry->color));
}
void fromwire_route_hop(const u8 **pptr, size_t *max, struct route_hop *entry)

View File

@@ -16,7 +16,7 @@ struct gossip_getnodes_entry {
u8 *globalfeatures;
s64 last_timestamp; /* -1 means never: following fields ignored */
struct wireaddr *addresses;
u8 *alias;
u8 alias[32];
u8 color[3];
};