From 19d37e54a010c38b18f6a3e8b577b6b016ce5a6b Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Tue, 7 Sep 2021 10:10:50 -0400 Subject: [PATCH] ospfd: Don't allocate json memory unless we have json output Several functions in ospf_vty.c were allocating json memory irrelevant if it was needed or not and then at the end of the loop free'ing it if it was not used. Clean up the access pattern. Signed-off-by: Donald Sharp --- ospfd/ospf_vty.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/ospfd/ospf_vty.c b/ospfd/ospf_vty.c index 8af4494335..3ae9707f5f 100644 --- a/ospfd/ospf_vty.c +++ b/ospfd/ospf_vty.c @@ -10607,9 +10607,10 @@ static void show_ip_ospf_route_network(struct vty *vty, struct ospf *ospf, prefix2str(&rn->p, buf1, sizeof(buf1)); - json_route = json_object_new_object(); - if (json) + if (json) { + json_route = json_object_new_object(); json_object_object_add(json, buf1, json_route); + } switch (or->path_type) { case OSPF_PATH_INTER_AREA: @@ -10730,8 +10731,6 @@ static void show_ip_ospf_route_network(struct vty *vty, struct ospf *ospf, } } } - if (!json) - json_object_free(json_route); } if (!json) vty_out(vty, "\n"); @@ -10759,8 +10758,8 @@ static void show_ip_ospf_route_router(struct vty *vty, struct ospf *ospf, continue; int flag = 0; - json_route = json_object_new_object(); if (json) { + json_route = json_object_new_object(); json_object_object_add( json, inet_ntop(AF_INET, &rn->p.u.prefix4, buf, sizeof(buf)), @@ -10875,8 +10874,6 @@ static void show_ip_ospf_route_router(struct vty *vty, struct ospf *ospf, } } } - if (!json) - json_object_free(json_route); } if (!json) vty_out(vty, "\n"); @@ -10905,9 +10902,10 @@ static void show_ip_ospf_route_external(struct vty *vty, struct ospf *ospf, char buf1[19]; snprintfrr(buf1, sizeof(buf1), "%pFX", &rn->p); - json_route = json_object_new_object(); - if (json) + if (json) { + json_route = json_object_new_object(); json_object_object_add(json, buf1, json_route); + } switch (er->path_type) { case OSPF_PATH_TYPE1_EXTERNAL: @@ -11004,8 +11002,6 @@ static void show_ip_ospf_route_external(struct vty *vty, struct ospf *ospf, } } } - if (!json) - json_object_free(json_route); } if (!json) vty_out(vty, "\n"); -- 2.39.5