From 74a630b606143b88f2d0db18c0b8c8530e4d0ae4 Mon Sep 17 00:00:00 2001 From: Naveen Thanikachalam Date: Tue, 7 Apr 2020 05:03:34 -0700 Subject: [PATCH] bgpd: Fixes for memory leaks. This commit addresses the memory leaks when certain BGP JSON show commands are executed Signed-off-by: NaveenThanikachalam --- bgpd/bgp_lcommunity.c | 2 ++ bgpd/bgp_route.c | 10 ++++++++-- bgpd/bgp_vty.c | 37 ++++++++++++++++++++++--------------- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/bgpd/bgp_lcommunity.c b/bgpd/bgp_lcommunity.c index ec7d07fe73..f47ae91663 100644 --- a/bgpd/bgp_lcommunity.c +++ b/bgpd/bgp_lcommunity.c @@ -46,6 +46,8 @@ void lcommunity_free(struct lcommunity **lcom) { XFREE(MTYPE_LCOMMUNITY_VAL, (*lcom)->val); XFREE(MTYPE_LCOMMUNITY_STR, (*lcom)->str); + if ((*lcom)->json) + json_object_free((*lcom)->json); XFREE(MTYPE_LCOMMUNITY, *lcom); } diff --git a/bgpd/bgp_route.c b/bgpd/bgp_route.c index a11e1d7c69..94e6cd27e2 100644 --- a/bgpd/bgp_route.c +++ b/bgpd/bgp_route.c @@ -11594,8 +11594,8 @@ static void show_adj_route(struct vty *vty, struct peer *peer, afi_t afi, struct bgp_table *table; struct bgp_adj_in *ain; struct bgp_adj_out *adj; - unsigned long output_count; - unsigned long filtered_count; + unsigned long output_count = 0; + unsigned long filtered_count = 0; struct bgp_node *rn; int header1 = 1; struct bgp *bgp; @@ -11885,6 +11885,12 @@ static void show_adj_route(struct vty *vty, struct peer *peer, afi_t afi, vty_out(vty, "%s\n", json_object_to_json_string_ext( json, JSON_C_TO_STRING_PRETTY)); + + if (!output_count && !filtered_count) { + json_object_free(json_scode); + json_object_free(json_ocode); + } + json_object_free(json); } else if (output_count > 0) { if (filtered_count > 0) diff --git a/bgpd/bgp_vty.c b/bgpd/bgp_vty.c index bfa3ee92a0..8f06fdf86c 100644 --- a/bgpd/bgp_vty.c +++ b/bgpd/bgp_vty.c @@ -12192,14 +12192,20 @@ static int bgp_show_neighbor_graceful_restart(struct vty *vty, struct bgp *bgp, enum show_type type, union sockunion *su, const char *conf_if, afi_t afi, - bool use_json, json_object *json) + bool use_json) { struct listnode *node, *nnode; struct peer *peer; int find = 0; safi_t safi = SAFI_UNICAST; + json_object *json = NULL; json_object *json_neighbor = NULL; + if (use_json) { + json = json_object_new_object(); + json_neighbor = json_object_new_object(); + } + for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)) @@ -12208,16 +12214,15 @@ static int bgp_show_neighbor_graceful_restart(struct vty *vty, struct bgp *bgp, if ((peer->afc[afi][safi]) == 0) continue; - if (use_json) - json_neighbor = json_object_new_object(); - if (type == show_all) { bgp_show_peer_gr_status(vty, peer, use_json, json_neighbor); - if (use_json) + if (use_json) { json_object_object_add(json, peer->host, json_neighbor); + json_neighbor = NULL; + } } else if (type == show_peer) { if (conf_if) { @@ -12243,8 +12248,10 @@ static int bgp_show_neighbor_graceful_restart(struct vty *vty, struct bgp *bgp, json_neighbor); } - if (find) + if (find) { + json_neighbor = NULL; break; + } } if (type == show_peer && !find) { @@ -12257,6 +12264,10 @@ static int bgp_show_neighbor_graceful_restart(struct vty *vty, struct bgp *bgp, vty_out(vty, "%s\n", json_object_to_json_string_ext( json, JSON_C_TO_STRING_PRETTY)); + + if (json_neighbor) + json_object_free(json_neighbor); + json_object_free(json); } else { vty_out(vty, "\n"); } @@ -12378,7 +12389,6 @@ static void bgp_show_neighbor_graceful_restart_vty(struct vty *vty, int ret; struct bgp *bgp; union sockunion su; - json_object *json = NULL; bgp = bgp_get_default(); @@ -12389,20 +12399,17 @@ static void bgp_show_neighbor_graceful_restart_vty(struct vty *vty, bgp_show_global_graceful_restart_mode_vty(vty, bgp, use_json, NULL); - json = json_object_new_object(); if (ip_str) { ret = str2sockunion(ip_str, &su); if (ret < 0) - bgp_show_neighbor_graceful_restart(vty, bgp, type, NULL, - ip_str, afi, - use_json, json); - else bgp_show_neighbor_graceful_restart( - vty, bgp, type, &su, NULL, afi, use_json, json); + vty, bgp, type, NULL, ip_str, afi, use_json); + else + bgp_show_neighbor_graceful_restart(vty, bgp, type, &su, + NULL, afi, use_json); } else bgp_show_neighbor_graceful_restart(vty, bgp, type, NULL, NULL, - afi, use_json, json); - json_object_free(json); + afi, use_json); } static void bgp_show_all_instances_neighbors_vty(struct vty *vty,