From b6df4090322446a2ee32fc5e2d43d074d34a8f1f Mon Sep 17 00:00:00 2001 From: Don Slice Date: Fri, 16 Sep 2016 09:20:03 -0700 Subject: [PATCH] bgpd: resolve memory leaks in "show ip bgp neighbor json" Found several leaks in bgp_show_peer and bgp_show_peer_afi where json objects are created and then not attached to the parent, causing them to be leaked. If not attaching them, freeing the created objects. Manual testing performed successfully. Fix tested succesfully by the submitter and bgp-smoke completed with same failures as base. Ticket: CM-12846 Signed-off-by: Don Slice Reviewed-by: CCR-5181 --- bgpd/bgp_vty.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index f16cf53cbe..04bb81548a 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -10971,8 +10971,6 @@ bgp_show_peer_afi (struct vty *vty, struct peer *p, afi_t afi, safi_t safi, { json_addr = json_object_new_object(); json_af = json_object_new_object(); - json_prefA = json_object_new_object(); - json_prefB = json_object_new_object(); filter = &p->filter[afi][safi]; if (peer_group_active(p)) @@ -10992,6 +10990,7 @@ bgp_show_peer_afi (struct vty *vty, struct peer *p, afi_t afi, safi_t safi, || CHECK_FLAG (p->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV)) { json_object_int_add(json_af, "orfType", ORF_TYPE_PREFIX); + json_prefA = json_object_new_object(); bgp_show_peer_afi_orf_cap (vty, p, afi, safi, PEER_CAP_ORF_PREFIX_SM_ADV, PEER_CAP_ORF_PREFIX_RM_ADV, @@ -11006,6 +11005,7 @@ bgp_show_peer_afi (struct vty *vty, struct peer *p, afi_t afi, safi_t safi, || CHECK_FLAG (p->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV)) { json_object_int_add(json_af, "orfOldType", ORF_TYPE_PREFIX_OLD); + json_prefB = json_object_new_object(); bgp_show_peer_afi_orf_cap (vty, p, afi, safi, PEER_CAP_ORF_PREFIX_SM_ADV, PEER_CAP_ORF_PREFIX_RM_ADV, @@ -11021,6 +11021,8 @@ bgp_show_peer_afi (struct vty *vty, struct peer *p, afi_t afi, safi_t safi, || CHECK_FLAG (p->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_RCV) || CHECK_FLAG (p->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_OLD_RCV)) json_object_object_add(json_addr, "afDependentCap", json_af); + else + json_object_free(json_af); sprintf (orf_pfx_name, "%s.%d.%d", p->host, afi, safi); orf_pfx_count = prefix_bgp_show_prefix_list (NULL, afi, orf_pfx_name, use_json); @@ -11743,6 +11745,8 @@ bgp_show_peer (struct vty *vty, struct peer *p, u_char use_json, json_object *js CHECK_FLAG (p->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_ADV) || CHECK_FLAG (p->af_cap[afi][safi], PEER_CAP_ADDPATH_AF_RX_RCV)) json_object_object_add(json_add, print_store, json_sub); + else + json_object_free(json_sub); } json_object_object_add(json_cap, "addPath", json_add); @@ -11767,7 +11771,6 @@ bgp_show_peer (struct vty *vty, struct peer *p, u_char use_json, json_object *js json_object *json_nxt = NULL; const char *print_store; - json_nxt = json_object_new_object(); if (CHECK_FLAG (p->cap, PEER_CAP_ENHE_ADV) && CHECK_FLAG (p->cap, PEER_CAP_ENHE_RCV)) json_object_string_add(json_cap, "extendedNexthop", "advertisedAndReceived"); @@ -11778,6 +11781,8 @@ bgp_show_peer (struct vty *vty, struct peer *p, u_char use_json, json_object *js if (CHECK_FLAG (p->cap, PEER_CAP_ENHE_RCV)) { + json_nxt = json_object_new_object(); + for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) { if (CHECK_FLAG (p->af_cap[AFI_IP][safi], PEER_CAP_ENHE_AF_RCV)) @@ -11875,7 +11880,10 @@ bgp_show_peer (struct vty *vty, struct peer *p, u_char use_json, json_object *js } } if (! restart_af_count) - json_object_string_add(json_cap, "addressFamiliesByPeer", "none"); + { + json_object_string_add(json_cap, "addressFamiliesByPeer", "none"); + json_object_free(json_restart); + } else json_object_object_add(json_cap, "addressFamiliesByPeer", json_restart); } -- 2.39.5