From 432be0a10329c3209610a3af932209819b5999b5 Mon Sep 17 00:00:00 2001 From: Richard Nyberg Date: Tue, 3 Feb 2009 23:11:34 +0100 Subject: [PATCH] Make net->piece_count properly aligned. The misalignment caused btpd to not work properly on machines like the NSLU2. Reported by John Caldwell. --- btpd/net.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/btpd/net.c b/btpd/net.c index 85739b3..3e76cb7 100644 --- a/btpd/net.c +++ b/btpd/net.c @@ -41,23 +41,21 @@ net_torrent_has_peer(struct net *n, const uint8_t *id) void net_create(struct torrent *tp) { - size_t field_size = ceil(tp->npieces / 8.0); - size_t mem = sizeof(*(tp->net)) + field_size + - tp->npieces * sizeof(*(tp->net->piece_count)); - - struct net *n = btpd_calloc(1, mem); + struct net *n = btpd_calloc(1, sizeof(*n)); n->tp = tp; tp->net = n; BTPDQ_INIT(&n->getlst); - - n->busy_field = (uint8_t *)(n + 1); - n->piece_count = (unsigned *)(n->busy_field + field_size); + + n->busy_field = btpd_calloc(ceil(tp->npieces / 8.0), 1); + n->piece_count = btpd_calloc(tp->npieces, sizeof(*n->piece_count)); } void net_kill(struct torrent *tp) { + free(tp->net->piece_count); + free(tp->net->busy_field); free(tp->net); tp->net = NULL; }