netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hangbin Liu <liuhangbin@gmail.com>
To: Michal Kubecek <mkubecek@suse.cz>
Cc: netdev@vger.kernel.org,
	Stephen Hemminger <stephen@networkplumber.org>,
	Phil Sutter <phil@nwl.cc>
Subject: Re: [PATCHv2 iproute2 1/2] lib/libnetlink: re malloc buff if size is not enough
Date: Tue, 19 Sep 2017 11:05:20 +0800	[thread overview]
Message-ID: <20170919030520.GL5465@leo.usersys.redhat.com> (raw)
In-Reply-To: <20170918075504.gdx4q7je5uneefgg@unicorn.suse.cz>

Hi Michal,

On Mon, Sep 18, 2017 at 09:55:05AM +0200, Michal Kubecek wrote:
> > +static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
> > +{
> > +	struct iovec *iov;
> > +	int len = -1, buf_len = 32768;
> > +	char *bufp, *buf = NULL;
> > +
> > +	int flag = MSG_PEEK | MSG_TRUNC;
> > +
> > +realloc:
> > +	bufp = realloc(buf, buf_len);
> > +
> > +	if (bufp == NULL) {
> > +		fprintf(stderr, "malloc error: not enough buffer\n");
> > +		free(buf);
> > +		return -ENOMEM;
> > +	}
> > +	buf = bufp;
> > +	iov = msg->msg_iov;
> > +	iov->iov_base = buf;
> > +	iov->iov_len = buf_len;
> > +
> > +recv:
> > +	len = recvmsg(fd, msg, flag);
> > +
> > +	if (len < 0) {
> > +		if (errno == EINTR || errno == EAGAIN)
> > +			goto recv;
> > +		fprintf(stderr, "netlink receive error %s (%d)\n",
> > +			strerror(errno), errno);
> 
> free(buf);
> 
> > +		return len;
> 
> Maybe we should return -errno (saved before calling fprintf()) to be
> consistent.
> 
> > +	}
> > +
> > +	if (len == 0) {
> > +		fprintf(stderr, "EOF on netlink\n");
> 
> free(buf);

Will fix these three issues.

> > @@ -471,19 +516,23 @@ int rtnl_dump_filter_l(struct rtnl_handle *rth,
> >  
> >  				if (h->nlmsg_type == NLMSG_ERROR) {
> >  					rtnl_dump_error(rth, h);
> > +					free(buf);
> >  					return -1;
> >  				}
> >  
> >  				if (!rth->dump_fp) {
> >  					err = a->filter(&nladdr, h, a->arg1);
> > -					if (err < 0)
> > +					if (err < 0) {
> > +						free(buf);
> >  						return err;
> > +					}
> >  				}
> >  
> >  skip_it:
> >  				h = NLMSG_NEXT(h, msglen);
> >  			}
> >  		}
> > +		free(buf);
> 
> We only free the last buffer returned by rtnl_recvmsg() this way. IMHO
> this free(buf) should be moved inside the loop.

Do you mean the outside while loop or the for loop? I think we could not put
it inside the for loop, because we may need the buf multi times based on arg.

	while (1) {
		status = rtnl_recvmsg(rth->fd, &msg, &buf);

		for (a = arg; a->filter; a++) {
			struct nlmsghdr *h = (struct nlmsghdr *)buf;
			while (NLMSG_OK(h, msglen)) {
				[...]
skip_it:
				h = NLMSG_NEXT(h, msglen);
			}
		}
		free(buf);
		[...]
	}

Thanks
Hangbin

  reply	other threads:[~2017-09-19  3:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-13  9:59 [PATCHv2 iproute2 0/2] libnetlink: malloc correct buff at run time Hangbin Liu
2017-09-13  9:59 ` [PATCHv2 iproute2 1/2] lib/libnetlink: re malloc buff if size is not enough Hangbin Liu
2017-09-18  7:55   ` Michal Kubecek
2017-09-19  3:05     ` Hangbin Liu [this message]
2017-09-19 11:48       ` Michal Kubecek
2017-09-13  9:59 ` [PATCHv2 iproute2 2/2] lib/libnetlink: update rtnl_talk to support malloc buff at run time Hangbin Liu

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=20170919030520.GL5465@leo.usersys.redhat.com \
    --to=liuhangbin@gmail.com \
    --cc=mkubecek@suse.cz \
    --cc=netdev@vger.kernel.org \
    --cc=phil@nwl.cc \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).