From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752891AbcHLVzu (ORCPT ); Fri, 12 Aug 2016 17:55:50 -0400 Received: from mail.kernel.org ([198.145.29.136]:51632 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751154AbcHLVzY (ORCPT ); Fri, 12 Aug 2016 17:55:24 -0400 Date: Fri, 12 Aug 2016 18:55:17 -0300 From: Arnaldo Carvalho de Melo To: Colin King Cc: Peter Zijlstra , Ingo Molnar , Alexander Shishkin , Masami Hiramatsu , Namhyung Kim , Jiri Olsa , Wang Nan , linux-kernel@vger.kernel.org Subject: Re: [PATCH] perf probe: check for dup and fdopen failures Message-ID: <20160812215517.GO27651@kernel.org> References: <1471038296-12956-1-git-send-email-colin.king@canonical.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1471038296-12956-1-git-send-email-colin.king@canonical.com> X-Url: http://acmel.wordpress.com User-Agent: Mutt/1.6.2 (2016-07-01) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Fri, Aug 12, 2016 at 10:44:56PM +0100, Colin King escreveu: > From: Colin Ian King > > dup and fdopen can potentially fail, so add some extra > error handling checks rather than assuming they always work. > > Signed-off-by: Colin Ian King > --- > 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? - 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