From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by dpdk.org (Postfix) with ESMTP id 1FF9C7CEE for ; Fri, 18 Aug 2017 16:20:05 +0200 (CEST) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP; 18 Aug 2017 07:20:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.41,393,1498546800"; d="scan'208";a="1184270210" Received: from gklab-246-025.igk.intel.com (HELO Sent) ([10.217.246.25]) by fmsmga001.fm.intel.com with SMTP; 18 Aug 2017 07:20:02 -0700 Received: by Sent (sSMTP sendmail emulation); Fri, 18 Aug 2017 16:17:38 +0200 From: Daniel Mrzyglod To: beilei.xing@intel.com Cc: dev@dpdk.org, Daniel Mrzyglod Date: Fri, 18 Aug 2017 16:17:36 +0200 Message-Id: <20170818141736.4769-1-danielx.t.mrzyglod@intel.com> X-Mailer: git-send-email 2.13.5 Subject: [dpdk-dev] [PATCH] app/testpmd: wrong usage of fseek & ftell to determine filesize 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, 18 Aug 2017 14:20:06 -0000 This issue was about passing unsigned argument where should be signed number. In reality this is about wrong usage of fseek and ftell to determine filesize. This patch is compliant to suggestions from FIO19-C: "Do not use fseek() and ftell() to compute the size of a regular file" Coverity issue: 143454 Fixes: a92a5a2cbbff ("app/testpmd: add command for loading DDP") Signed-off-by: Daniel Mrzyglod --- app/test-pmd/config.c | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/app/test-pmd/config.c b/app/test-pmd/config.c index 3ae3e1cd8..32b0c1566 100644 --- a/app/test-pmd/config.c +++ b/app/test-pmd/config.c @@ -40,6 +40,10 @@ #include #include +#include +#include +#include +#include #include #include @@ -3295,46 +3299,43 @@ port_dcb_info_display(uint8_t port_id) uint8_t * open_ddp_package_file(const char *file_path, uint32_t *size) { - FILE *fh = fopen(file_path, "rb"); - uint32_t pkg_size; + int fd = open(file_path, O_RDONLY); + off_t pkg_size; uint8_t *buf = NULL; int ret = 0; + struct stat st_buf; if (size) *size = 0; - if (fh == NULL) { + if (fd == -1) { printf("%s: Failed to open %s\n", __func__, file_path); return buf; } - ret = fseek(fh, 0, SEEK_END); - if (ret < 0) { - fclose(fh); + if ((fstat(fd, &st_buf) != 0) || (!S_ISREG(st_buf.st_mode))) { + close(fd); printf("%s: File operations failed\n", __func__); return buf; } - pkg_size = ftell(fh); + pkg_size = st_buf.st_size; + if (pkg_size < 0) { + close(fd); + printf("%s: File operations failed\n", __func__); + return buf; + } buf = (uint8_t *)malloc(pkg_size); if (!buf) { - fclose(fh); + close(fd); printf("%s: Failed to malloc memory\n", __func__); return buf; } - ret = fseek(fh, 0, SEEK_SET); + ret = read(fd, buf, pkg_size); if (ret < 0) { - fclose(fh); - printf("%s: File seek operation failed\n", __func__); - close_ddp_package_file(buf); - return NULL; - } - - ret = fread(buf, 1, pkg_size, fh); - if (ret < 0) { - fclose(fh); + close(fd); printf("%s: File read operation failed\n", __func__); close_ddp_package_file(buf); return NULL; @@ -3343,7 +3344,7 @@ open_ddp_package_file(const char *file_path, uint32_t *size) if (size) *size = pkg_size; - fclose(fh); + close(fd); return buf; } -- 2.13.4