From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.tuxdriver.com (charlotte.tuxdriver.com [70.61.120.58]) by dpdk.org (Postfix) with ESMTP id 3F2622B92 for ; Fri, 2 Feb 2018 19:48:04 +0100 (CET) Received: from cpe-2606-a000-111b-4011-eaa3-4b92-4a68-8f24.dyn6.twc.com ([2606:a000:111b:4011:eaa3:4b92:4a68:8f24] helo=localhost) by smtp.tuxdriver.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.63) (envelope-from ) id 1ehgNO-0004Zp-84; Fri, 02 Feb 2018 13:48:01 -0500 Date: Fri, 2 Feb 2018 13:47:22 -0500 From: Neil Horman To: Bruce Richardson Cc: dev@dpdk.org Message-ID: <20180202184721.GC21773@hmswarspite.think-freely.org> References: <20180202120058.243184-1-bruce.richardson@intel.com> <20180202124439.GB21773@hmswarspite.think-freely.org> <20180202154743.GA20444@bricha3-MOBL3.ger.corp.intel.com> <20180202155112.GB20444@bricha3-MOBL3.ger.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180202155112.GB20444@bricha3-MOBL3.ger.corp.intel.com> User-Agent: Mutt/1.9.1 (2017-09-22) X-Spam-Score: -2.9 (--) X-Spam-Status: No Subject: Re: [dpdk-dev] [PATCH] pmdinfogen: fix resource leak of FILE object X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 Feb 2018 18:48:04 -0000 On Fri, Feb 02, 2018 at 03:51:12PM +0000, Bruce Richardson wrote: > On Fri, Feb 02, 2018 at 03:47:43PM +0000, Bruce Richardson wrote: > > On Fri, Feb 02, 2018 at 07:44:39AM -0500, Neil Horman wrote: > > > On Fri, Feb 02, 2018 at 12:00:58PM +0000, Bruce Richardson wrote: > > > > Coverity flags an issue where the resources used by the FILE object for > > > > the temporary input file are leaked. This is a very minor issue, but is > > > > easily fixed, while also avoiding later problems where we try to close > > > > an invalid file descriptor in the failure case. > > > > > > > > The fix is to use "dup()" to get a new file descriptor number rather than > > > > using the value directly from fileno. This allows us to close the file > > > > opened with tmpfile() within in scope block, while allowing the duplicate > > > > to pass to the outer block and be closed when the function terminates. > > > > > > > > As a side-effect I/O in the function is therefore changed from using stdio > > > > fread/fwrite to read/write system calls. > > > > > > > > Coverity issue: 260399 > > > > Fixes: 0d68533617e3 ("pmdinfogen: allow using stdin and stdout") > > > > > > > > Signed-off-by: Bruce Richardson > > > > --- > > > > buildtools/pmdinfogen/pmdinfogen.c | 16 ++++++++++------ > > > > 1 file changed, 10 insertions(+), 6 deletions(-) > > > > > > > > diff --git a/buildtools/pmdinfogen/pmdinfogen.c b/buildtools/pmdinfogen/pmdinfogen.c > > > > index 45b267346..0f35ca46b 100644 > > > > --- a/buildtools/pmdinfogen/pmdinfogen.c > > > > +++ b/buildtools/pmdinfogen/pmdinfogen.c > > > > @@ -50,20 +50,24 @@ static void *grab_file(const char *filename, unsigned long *size) > > > > /* from stdin, use a temporary file to mmap */ > > > > FILE *infile; > > > > char buffer[1024]; > > > > - size_t n; > > > > + int n; > > > > > > > > infile = tmpfile(); > > > > if (infile == NULL) { > > > > perror("tmpfile"); > > > > return NULL; > > > > } > > > > - while (!feof(stdin)) { > > > > - n = fread(buffer, 1, sizeof(buffer), stdin); > > > > - if (fwrite(buffer, 1, n, infile) != n) > > > > + fd = dup(fileno(infile)); > > > > + fclose(infile); > > > > + if (fd < 0) > > > > + return NULL; > > > > + > > > > + n = read(STDIN_FILENO, buffer, sizeof(buffer)); > > > > + while (n > 0) { > > > > + if (write(fd, buffer, n) != n) > > > > goto failed; > > > > + n = read(STDIN_FILENO, buffer, sizeof(buffer)); > > > > } > > > > - fflush(infile); > > > > - fd = fileno(infile); > > > > } > > > > > > > > if (fstat(fd, &st)) > > > > -- > > > > 2.14.3 > > > > > > > > > > > > > > Wouldn't it be just as good, and easier to check fd for == -1 as a condition of > > > calling close? > > > > > > like > > > failed: > > > if (fd >= 0) > > > close(fd); > > > > > That would fix the problem of calling goto failed with fd set to -1, but > > would not fix the resource issue that coverity was complaining about. We > > were allocating a stdio FILE object, then taking just the fileno of it > > and letting the file number go out of scope. This cleans this that up, > s/file number/FILE object ptr/ > Yeah, Ok, I can see that, though I still think its a bit of a false positive, since the definition of tmpfile says it will automatically unlink the file on process exit. No matter though, what you have is an improvement regardless. > > so that we just use file numbers and properly close the FILE * once it's > > outlived its usefulness. > > > > BTW: I did investigate using open and O_TMPFILE in place of tmpfile() > > call, but while it would work great on Linux, it's not available > > elsewhere, so tmpfile looks the best option. > > yeah, thats both OS and filesystem specific, I wouldn't trust it too much. Acked-by: Neil Horman > > Regards, > > /Bruce >