bgpd: Fixes for memory leaks.

This commit addresses the memory leaks when certain BGP JSON
show commands are executed

Signed-off-by: NaveenThanikachalam <nthanikachal@vmware.com>
This commit is contained in:
Naveen Thanikachalam 2020-04-07 05:03:34 -07:00
parent 7611871dfc
commit 74a630b606
3 changed files with 32 additions and 17 deletions

View File

@ -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);
}

View File

@ -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)

View File

@ -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,