Browse Source

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>
fee-tracking2
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
afc92dd757
  1. 9
      gossipd/gossipd.c
  2. 6
      gossipd/routing.c
  3. 4
      gossipd/routing.h
  4. 5
      lightningd/gossip_control.c
  5. 13
      lightningd/gossip_msg.c
  6. 2
      lightningd/gossip_msg.h

9
gossipd/gossipd.c

@ -1,3 +1,4 @@
#include <ccan/array_size/array_size.h>
#include <ccan/asort/asort.h> #include <ccan/asort/asort.h>
#include <ccan/build_assert/build_assert.h> #include <ccan/build_assert/build_assert.h>
#include <ccan/cast/cast.h> #include <ccan/cast/cast.h>
@ -84,7 +85,7 @@ struct daemon {
/* Global features to list in node_announcement. */ /* Global features to list in node_announcement. */
u8 *globalfeatures; u8 *globalfeatures;
u8 alias[33]; u8 alias[32];
u8 rgb[3]; u8 rgb[3];
/* What we can actually announce. */ /* What we can actually announce. */
@ -1410,8 +1411,10 @@ static void append_node(const struct gossip_getnodes_entry ***nodes,
} else { } else {
new->last_timestamp = n->last_timestamp; new->last_timestamp = n->last_timestamp;
new->addresses = n->addresses; new->addresses = n->addresses;
new->alias = n->alias; BUILD_ASSERT(ARRAY_SIZE(new->alias) == ARRAY_SIZE(n->alias));
memcpy(new->color, n->rgb_color, 3); BUILD_ASSERT(ARRAY_SIZE(new->color) == ARRAY_SIZE(n->rgb_color));
memcpy(new->alias, n->alias, ARRAY_SIZE(new->alias));
memcpy(new->color, n->rgb_color, ARRAY_SIZE(new->color));
} }
*tal_arr_expand(nodes) = new; *tal_arr_expand(nodes) = new;
} }

6
gossipd/routing.c

@ -145,7 +145,6 @@ static struct node *new_node(struct routing_state *rstate,
n = tal(rstate, struct node); n = tal(rstate, struct node);
n->id = *id; n->id = *id;
n->chans = tal_arr(n, struct chan *, 0); n->chans = tal_arr(n, struct chan *, 0);
n->alias = NULL;
n->gfeatures = NULL; n->gfeatures = NULL;
n->node_announcement = NULL; n->node_announcement = NULL;
n->node_announcement_index = 0; n->node_announcement_index = 0;
@ -1306,9 +1305,8 @@ bool routing_add_node_announcement(struct routing_state *rstate, const u8 *msg T
node->addresses = tal_steal(node, wireaddrs); node->addresses = tal_steal(node, wireaddrs);
node->last_timestamp = timestamp; node->last_timestamp = timestamp;
memcpy(node->rgb_color, rgb_color, 3); memcpy(node->rgb_color, rgb_color, ARRAY_SIZE(node->rgb_color));
tal_free(node->alias); memcpy(node->alias, alias, ARRAY_SIZE(node->alias));
node->alias = tal_dup_arr(node, u8, alias, 32, 0);
tal_free(node->gfeatures); tal_free(node->gfeatures);
node->gfeatures = tal_steal(node, features); node->gfeatures = tal_steal(node, features);

4
gossipd/routing.h

@ -110,8 +110,8 @@ struct node {
struct chan *prev; struct chan *prev;
} bfg[ROUTING_MAX_HOPS+1]; } bfg[ROUTING_MAX_HOPS+1];
/* UTF-8 encoded alias as tal_arr, not zero terminated */ /* UTF-8 encoded alias, not zero terminated */
u8 *alias; u8 alias[32];
/* Color to be used when displaying the name */ /* Color to be used when displaying the name */
u8 rgb_color[3]; u8 rgb_color[3];

5
lightningd/gossip_control.c

@ -202,7 +202,10 @@ static void json_getnodes_reply(struct subd *gossip UNUSED, const u8 *reply,
json_object_end(response); json_object_end(response);
continue; 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_escaped_string(response, "alias", take(esc));
json_add_hex(response, "color", json_add_hex(response, "color",
nodes[i]->color, ARRAY_SIZE(nodes[i]->color)); nodes[i]->color, ARRAY_SIZE(nodes[i]->color));

13
lightningd/gossip_msg.c

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

2
lightningd/gossip_msg.h

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

Loading…
Cancel
Save