All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrea Claudi <aclaudi@redhat.com>
To: netdev@vger.kernel.org
Cc: stephen@networkplumber.org, dsahern@gmail.com
Subject: [PATCH iproute2] nexthop: fix memory leak in add_nh_group_attr()
Date: Wed, 14 Apr 2021 00:50:45 +0200	[thread overview]
Message-ID: <6ce757a509dc88b90b029cd7e9c96bb0a245a313.1618350667.git.aclaudi@redhat.com> (raw)

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: 63df8e8543b0 ("Add support for nexthop objects")
Signed-off-by: Andrea Claudi <aclaudi@redhat.com>
---
 ip/ipnexthop.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/ip/ipnexthop.c b/ip/ipnexthop.c
index 20cde586..f0658a9c 100644
--- a/ip/ipnexthop.c
+++ b/ip/ipnexthop.c
@@ -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)
 {
-	struct nexthop_grp *grps;
+	struct nexthop_grp *grps = NULL;
 	int count = 0, i;
+	int err = -1;
 	char *sep, *wsep;
 
 	if (*argv != '\0')
@@ -292,11 +293,11 @@ static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv)
 	}
 
 	if (count == 0)
-		return -1;
+		goto out;
 
 	grps = calloc(count, sizeof(*grps));
 	if (!grps)
-		return -1;
+		goto out;
 
 	for (i = 0; i < count; ++i) {
 		sep = strchr(argv, '/');
@@ -308,7 +309,7 @@ static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv)
 			*wsep = '\0';
 
 		if (get_unsigned(&grps[i].id, argv, 0))
-			return -1;
+			goto out;
 		if (wsep) {
 			unsigned int w;
 
@@ -324,7 +325,10 @@ static int add_nh_group_attr(struct nlmsghdr *n, int maxlen, char *argv)
 		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)
-- 
2.30.2


                 reply	other threads:[~2021-04-13 22:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6ce757a509dc88b90b029cd7e9c96bb0a245a313.1618350667.git.aclaudi@redhat.com \
    --to=aclaudi@redhat.com \
    --cc=dsahern@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=stephen@networkplumber.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.