iproute2: optimize code and fix some mem-leak risk

Signed-off-by: zhaoshuang <izhaoshuang@163.com>
Reviewed-by: Pawel Chmielewski <pawel.chmielewski@intel.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
zhaoshuang
2023-05-11 08:37:26 +08:00
committed by Stephen Hemminger
parent 465e87a89c
commit 7e8cdfa2ea
14 changed files with 34 additions and 12 deletions

View File

@@ -466,12 +466,14 @@ static int mdb_show(int argc, char **argv)
/* get mdb entries */
if (rtnl_mdbdump_req(&rth, PF_BRIDGE) < 0) {
perror("Cannot send dump request");
delete_json_obj();
return -1;
}
open_json_array(PRINT_JSON, "mdb");
if (rtnl_dump_filter(&rth, print_mdbs, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return -1;
}
close_json_array(PRINT_JSON, NULL);
@@ -479,12 +481,14 @@ static int mdb_show(int argc, char **argv)
/* get router ports */
if (rtnl_mdbdump_req(&rth, PF_BRIDGE) < 0) {
perror("Cannot send dump request");
delete_json_obj();
return -1;
}
open_json_object("router");
if (rtnl_dump_filter(&rth, print_rtrs, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return -1;
}
close_json_object();

View File

@@ -205,6 +205,14 @@ struct ifname_map {
char *ifname;
};
static void ifname_map_free(struct ifname_map *ifname_map)
{
free(ifname_map->ifname);
free(ifname_map->dev_name);
free(ifname_map->bus_name);
free(ifname_map);
}
static struct ifname_map *ifname_map_alloc(const char *bus_name,
const char *dev_name,
uint32_t port_index,
@@ -221,23 +229,12 @@ static struct ifname_map *ifname_map_alloc(const char *bus_name,
ifname_map->ifname = strdup(ifname);
if (!ifname_map->bus_name || !ifname_map->dev_name ||
!ifname_map->ifname) {
free(ifname_map->ifname);
free(ifname_map->dev_name);
free(ifname_map->bus_name);
free(ifname_map);
ifname_map_free(ifname_map);
return NULL;
}
return ifname_map;
}
static void ifname_map_free(struct ifname_map *ifname_map)
{
free(ifname_map->ifname);
free(ifname_map->dev_name);
free(ifname_map->bus_name);
free(ifname_map);
}
static int ifname_map_update(struct ifname_map *ifname_map, const char *ifname)
{
char *new_ifname;

View File

@@ -113,6 +113,7 @@ static int ipaddrlabel_list(int argc, char **argv)
new_json_obj(json);
if (rtnl_dump_filter(&rth, print_addrlabel, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();

View File

@@ -318,6 +318,7 @@ static int do_show(int argc, char **argv)
new_json_obj(json);
if (rtnl_dump_filter(&genl_rth, print_fou_mapping, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();

View File

@@ -150,6 +150,7 @@ static int do_list(int argc, char **argv)
new_json_obj(json);
if (rtnl_dump_filter(&genl_rth, print_ila_mapping, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();

View File

@@ -209,6 +209,7 @@ dump:
*/
if (errno == EOPNOTSUPP &&
filter.family == AF_UNSPEC) {
delete_json_obj();
filter.family = AF_INET;
goto dump;
}

View File

@@ -1018,6 +1018,7 @@ static int ipnh_get_id(__u32 id)
new_json_obj(json);
if (print_nexthop_nocache(answer, (void *)stdout) < 0) {
delete_json_obj();
free(answer);
return -1;
}
@@ -1103,6 +1104,7 @@ static int ipnh_list_flush(int argc, char **argv, int action)
new_json_obj(json);
if (rtnl_dump_filter(&rth, print_nexthop_nocache, stdout) < 0) {
delete_json_obj();
fprintf(stderr, "Dump terminated\n");
return -2;
}
@@ -1178,6 +1180,7 @@ static int ipnh_bucket_list(int argc, char **argv)
new_json_obj(json);
if (rtnl_dump_filter(&rth, print_nexthop_bucket, stdout) < 0) {
delete_json_obj();
fprintf(stderr, "Dump terminated\n");
return -2;
}
@@ -1218,6 +1221,7 @@ static int ipnh_bucket_get_id(__u32 id, __u16 bucket_index)
new_json_obj(json);
if (print_nexthop_bucket(answer, (void *)stdout) < 0) {
delete_json_obj();
free(answer);
return -1;
}

View File

@@ -1977,6 +1977,7 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
if (rtnl_dump_filter_errhndlr(&rth, filter_fn, stdout,
save_route_errhndlr, NULL) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return -2;
}
@@ -2172,18 +2173,21 @@ static int iproute_get(int argc, char **argv)
if (print_route(answer, (void *)stdout) < 0) {
fprintf(stderr, "An error :-)\n");
delete_json_obj();
free(answer);
return -1;
}
if (answer->nlmsg_type != RTM_NEWROUTE) {
fprintf(stderr, "Not a route?\n");
delete_json_obj();
free(answer);
return -1;
}
len -= NLMSG_LENGTH(sizeof(*r));
if (len < 0) {
fprintf(stderr, "Wrong len %d\n", len);
delete_json_obj();
free(answer);
return -1;
}
@@ -2195,6 +2199,7 @@ static int iproute_get(int argc, char **argv)
r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
} else if (!tb[RTA_SRC]) {
fprintf(stderr, "Failed to connect the route\n");
delete_json_obj();
free(answer);
return -1;
}
@@ -2217,6 +2222,7 @@ static int iproute_get(int argc, char **argv)
if (print_route(answer, (void *)stdout) < 0) {
fprintf(stderr, "An error :-)\n");
delete_json_obj();
free(answer);
return -1;
}

View File

@@ -714,6 +714,7 @@ static int iprule_list_flush_or_save(int argc, char **argv, int action)
new_json_obj(json);
if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();

View File

@@ -441,6 +441,7 @@ static int do_show(int argc, char **argv)
if (rtnl_dump_filter(&rth, print_tuntap, NULL) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return -1;
}

View File

@@ -419,11 +419,13 @@ int do_tunnels_list(struct tnl_print_nlmsg_info *info)
new_json_obj(json);
if (rtnl_linkdump_req(&rth, preferred_family) < 0) {
perror("Cannot send dump request\n");
delete_json_obj();
return -1;
}
if (rtnl_dump_filter(&rth, print_nlmsg_tunnel, info) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return -1;
}
delete_json_obj();

View File

@@ -453,6 +453,7 @@ static int tc_class_list(int argc, char **argv)
new_json_obj(json);
if (rtnl_dump_filter(&rth, print_class, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();

View File

@@ -734,6 +734,7 @@ static int tc_filter_list(int cmd, int argc, char **argv)
new_json_obj(json);
if (rtnl_dump_filter(&rth, print_filter, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();

View File

@@ -432,6 +432,7 @@ static int tc_qdisc_list(int argc, char **argv)
new_json_obj(json);
if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
delete_json_obj();
return 1;
}
delete_json_obj();