nexthop: fix memory leak in add_nh_group_attr()

grps is dinamically allocated with a calloc, and not freed in a return
path in the for cycle. This commit fix it.

While at it, make the function use a single return point.

Fixes: 63df8e8543 ("Add support for nexthop objects")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
This commit is contained in:
Andrea Claudi 2021-04-14 00:50:45 +02:00 committed by Stephen Hemminger
parent 6801ae8273
commit 6a2c51da99
1 changed files with 9 additions and 5 deletions

View File

@ -277,8 +277,9 @@ int print_nexthop(struct nlmsghdr *n, void *arg)
static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv) static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv)
{ {
struct nexthop_grp *grps; struct nexthop_grp *grps = NULL;
int count = 0, i; int count = 0, i;
int err = -1;
char *sep, *wsep; char *sep, *wsep;
if (*argv != '\0') if (*argv != '\0')
@ -292,11 +293,11 @@ static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv)
} }
if (count == 0) if (count == 0)
return -1; goto out;
grps = calloc(count, sizeof(*grps)); grps = calloc(count, sizeof(*grps));
if (!grps) if (!grps)
return -1; goto out;
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
sep = strchr(argv, '/'); sep = strchr(argv, '/');
@ -308,7 +309,7 @@ static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv)
*wsep = '\0'; *wsep = '\0';
if (get_unsigned(&grps[i].id, argv, 0)) if (get_unsigned(&grps[i].id, argv, 0))
return -1; goto out;
if (wsep) { if (wsep) {
unsigned int w; unsigned int w;
@ -324,7 +325,10 @@ static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv)
argv = sep + 1; argv = sep + 1;
} }
return addattr_l(n, maxlen, NHA_GROUP, grps, count * sizeof(*grps)); err = addattr_l(n, maxlen, NHA_GROUP, grps, count * sizeof(*grps));
out:
free(grps);
return err;
} }
static int ipnh_modify(int cmd, unsigned int flags, int argc, char **argv) static int ipnh_modify(int cmd, unsigned int flags, int argc, char **argv)