From patchwork Fri Feb 2 12:00:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruce Richardson X-Patchwork-Id: 34879 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id BFCBAA499; Fri, 2 Feb 2018 13:01:35 +0100 (CET) Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by dpdk.org (Postfix) with ESMTP id E41F17CEA for ; Fri, 2 Feb 2018 13:01:33 +0100 (CET) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Feb 2018 04:01:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,448,1511856000"; d="scan'208";a="14949105" Received: from silpixa00399126.ir.intel.com (HELO silpixa00399126.ger.corp.intel.com) ([10.237.223.223]) by fmsmga008.fm.intel.com with ESMTP; 02 Feb 2018 04:01:31 -0800 From: Bruce Richardson To: Neil Horman Cc: dev@dpdk.org, Bruce Richardson Date: Fri, 2 Feb 2018 12:00:58 +0000 Message-Id: <20180202120058.243184-1-bruce.richardson@intel.com> X-Mailer: git-send-email 2.14.3 Subject: [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: , Errors-To: dev-bounces@dpdk.org Sender: "dev" 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 Acked-by: Neil Horman --- 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))