Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1461530

Re: [PATCH] perf probe: check for dup and fdopen failures

From Masami Hiramatsu <mhiramat@kernel.org>
Newsgroups linux.kernel
Subject Re: [PATCH] perf probe: check for dup and fdopen failures
Date 2016-08-13 02:00 +0200
Message-ID <s5uwN-ac-3@gated-at.bofh.it> (permalink)
References <s5suZ-7jU-17@gated-at.bofh.it> <s5sEG-7nQ-27@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Fri, 12 Aug 2016 18:55:17 -0300
Arnaldo Carvalho de Melo <acme@kernel.org> wrote:

> Em Fri, Aug 12, 2016 at 10:44:56PM +0100, Colin King escreveu:
> > From: Colin Ian King <colin.king@canonical.com>
> > 
> > dup and fdopen can potentially fail, so add some extra
> > error handling checks rather than assuming they always work.

Thanks, I forgot to handle that.

> > 
> > Signed-off-by: Colin Ian King <colin.king@canonical.com>
> > ---
> >  tools/perf/util/probe-file.c | 16 ++++++++++++----
> >  1 file changed, 12 insertions(+), 4 deletions(-)
> > 
> > diff --git a/tools/perf/util/probe-file.c b/tools/perf/util/probe-file.c
> > index 9aed9c3..f3df939 100644
> > --- a/tools/perf/util/probe-file.c
> > +++ b/tools/perf/util/probe-file.c
> > @@ -133,7 +133,7 @@ int probe_file__open_both(int *kfd, int *ufd, int flag)
> >  /* Get raw string list of current kprobe_events  or uprobe_events */
> >  struct strlist *probe_file__get_rawlist(int fd)
> >  {
> > -	int ret, idx;
> > +	int ret, idx, fddup;
> >  	FILE *fp;
> >  	char buf[MAX_CMDLEN];
> >  	char *p;
> > @@ -144,7 +144,12 @@ struct strlist *probe_file__get_rawlist(int fd)
> >  
> >  	sl = strlist__new(NULL, NULL);
> >  
> > -	fp = fdopen(dup(fd), "r");
> > +	fddup = dup(fd);
> > +	if (fddup < 0)
> > +		return NULL;
> > +	fp = fdopen(fddup, "r");
> > +	if (!fp)
> 
> here we leak fddup, no?
> 
> > +		return NULL;
> 
> I.e. this should be:
> 
> 	if (!fp) {
> 		close(fddup);
> 		return NULL;
> 	}
> 
> Ack?

Correct, it should be closed. So both fixes look good to me.

Acked-by: Masami Hiramatsu <mhiramat@kernel.org>

Thanks!


> 
> - Arnaldo
> 
> >  	while (!feof(fp)) {
> >  		p = fgets(buf, MAX_CMDLEN, fp);
> >  		if (!p)
> > @@ -447,10 +452,13 @@ static int probe_cache__load(struct probe_cache *pcache)
> >  {
> >  	struct probe_cache_entry *entry = NULL;
> >  	char buf[MAX_CMDLEN], *p;
> > -	int ret = 0;
> > +	int ret = 0, fddup;
> >  	FILE *fp;
> >  
> > -	fp = fdopen(dup(pcache->fd), "r");
> > +	fddup = dup(pcache->fd);
> > +	if (fddup < 0)
> > +		return -errno;
> > +	fp = fdopen(fddup, "r");
> >  	if (!fp)
> >  		return -EINVAL;
> >  
> > -- 
> > 2.8.1


-- 
Masami Hiramatsu <mhiramat@kernel.org>

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH] perf probe: check for dup and fdopen failures Colin King <colin.king@canonical.com> - 2016-08-12 23:50 +0200
  Re: [PATCH] perf probe: check for dup and fdopen failures Arnaldo Carvalho de Melo <acme@kernel.org> - 2016-08-13 00:00 +0200
    Re: [PATCH] perf probe: check for dup and fdopen failures Masami Hiramatsu <mhiramat@kernel.org> - 2016-08-13 02:00 +0200
  [tip:perf/urgent] perf probe: Check for dup and fdopen failures tip-bot for Colin Ian King <tipbot@zytor.com> - 2016-08-16 20:20 +0200

csiph-web