From 37d361e7fd6b35dc4badd7e0124e2f49c4ac9e5e Mon Sep 17 00:00:00 2001 From: Renato Westphal Date: Fri, 21 Oct 2016 20:13:51 -0200 Subject: [PATCH] bgpd: plug several memleaks Signed-off-by: Renato Westphal --- bgpd/bgp_nexthop.c | 8 +++++++- bgpd/bgp_zebra.c | 1 + bgpd/bgpd.c | 20 ++++++++++++-------- tests/bgp_mpath_test.c | 2 ++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/bgpd/bgp_nexthop.c b/bgpd/bgp_nexthop.c index 46ccd06c4b..2d3d8e6a65 100644 --- a/bgpd/bgp_nexthop.c +++ b/bgpd/bgp_nexthop.c @@ -113,6 +113,12 @@ bgp_address_hash_alloc (void *p) return addr; } +static void +bgp_address_hash_free (void *addr) +{ + XFREE (MTYPE_BGP_ADDR, addr); +} + static unsigned int bgp_address_hash_key_make (void *p) { @@ -142,7 +148,7 @@ bgp_address_destroy (struct bgp *bgp) { if (bgp->address_hash == NULL) return; - hash_clean(bgp->address_hash, NULL); + hash_clean(bgp->address_hash, bgp_address_hash_free); hash_free(bgp->address_hash); bgp->address_hash = NULL; } diff --git a/bgpd/bgp_zebra.c b/bgpd/bgp_zebra.c index 8ecd025397..3d3bd90f5b 100644 --- a/bgpd/bgp_zebra.c +++ b/bgpd/bgp_zebra.c @@ -1792,6 +1792,7 @@ bgp_redist_del (struct bgp *bgp, afi_t afi, u_char type, u_short instance) if (red) { listnode_delete(bgp->redist[afi][type], red); + XFREE (MTYPE_BGP_REDIST, red); if (!bgp->redist[afi][type]->count) { list_free(bgp->redist[afi][type]); diff --git a/bgpd/bgpd.c b/bgpd/bgpd.c index 3ddb465a75..a786271541 100644 --- a/bgpd/bgpd.c +++ b/bgpd/bgpd.c @@ -1047,6 +1047,12 @@ peer_free (struct peer *peer) peer->host = NULL; } + if (peer->domainname) + { + XFREE (MTYPE_BGP_PEER_HOST, peer->domainname); + peer->domainname = NULL; + } + if (peer->ifname) { XFREE(MTYPE_BGP_PEER_IFNAME, peer->ifname); @@ -3123,6 +3129,7 @@ bgp_delete (struct bgp *bgp) struct peer *peer; struct peer_group *group; struct listnode *node, *next; + struct vrf *vrf; afi_t afi; int i; @@ -3186,8 +3193,6 @@ bgp_delete (struct bgp *bgp) * routes to be processed still referencing the struct bgp. */ listnode_delete (bm->bgp, bgp); - if (list_isempty(bm->bgp)) - bgp_close (); /* Deregister from Zebra, if needed */ if (IS_BGP_INST_KNOWN_TO_ZEBRA(bgp)) @@ -3196,6 +3201,10 @@ bgp_delete (struct bgp *bgp) /* Free interfaces in this instance. */ bgp_if_finish (bgp); + vrf = bgp_vrf_lookup_by_instance_type (bgp); + if (vrf) + bgp_vrf_unlink (bgp, vrf); + thread_master_free_unused(bm->master); bgp_unlock(bgp); /* initial reference */ @@ -3223,7 +3232,6 @@ bgp_free (struct bgp *bgp) { afi_t afi; safi_t safi; - struct vrf *vrf; list_delete (bgp->group); list_delete (bgp->peer); @@ -3245,13 +3253,9 @@ bgp_free (struct bgp *bgp) bgp_table_finish (&bgp->rib[afi][safi]); } + bgp_scan_finish (bgp); bgp_address_destroy (bgp); - /* If Default instance or VRF, unlink from the VRF structure. */ - vrf = bgp_vrf_lookup_by_instance_type (bgp); - if (vrf) - bgp_vrf_unlink (bgp, vrf); - if (bgp->name) XFREE(MTYPE_BGP, bgp->name); diff --git a/tests/bgp_mpath_test.c b/tests/bgp_mpath_test.c index dbcb00a2ef..723f2977d5 100644 --- a/tests/bgp_mpath_test.c +++ b/tests/bgp_mpath_test.c @@ -37,6 +37,7 @@ #include "bgpd/bgp_table.h" #include "bgpd/bgp_route.h" #include "bgpd/bgp_attr.h" +#include "bgpd/bgp_nexthop.h" #include "bgpd/bgp_mpath.h" #define VT100_RESET "\x1b[0m" @@ -116,6 +117,7 @@ bgp_create_fake (as_t *as, const char *name) bgp->maxpaths[afi][safi].maxpaths_ibgp = MULTIPATH_NUM; } + bgp_scan_init (bgp); bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF; bgp->default_holdtime = BGP_DEFAULT_HOLDTIME; bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE; -- 2.39.5