From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by dpdk.org (Postfix) with ESMTP id 4A44A6CA9 for ; Thu, 12 May 2016 14:41:47 +0200 (CEST) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga102.jf.intel.com with ESMTP; 12 May 2016 05:41:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.24,610,1455004800"; d="scan'208";a="964442488" Received: from gklab-246-018.igk.intel.com (HELO stargo) ([10.217.246.18]) by fmsmga001.fm.intel.com with SMTP; 12 May 2016 05:41:35 -0700 Received: by stargo (sSMTP sendmail emulation); Thu, 12 May 2016 14:46:56 +0200 From: Slawomir Mrozowicz To: olivier.matz@6wind.com Cc: dev@dpdk.org, Slawomir Mrozowicz Date: Thu, 12 May 2016 14:46:53 +0200 Message-Id: <1463057213-4123-1-git-send-email-slawomirx.mrozowicz@intel.com> X-Mailer: git-send-email 1.9.1 Subject: [dpdk-dev] [PATCH] rte mempool: division or modulo by zero X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 May 2016 12:41:47 -0000 Fix issue reported by Coverity. Coverity ID 13243: Division or modulo by zero In function call rte_mempool_xmem_size, division by expression total_size which may be zero has undefined behavior. Fixes: 148f963fb532 ("xen: core library changes") Signed-off-by: Slawomir Mrozowicz --- lib/librte_mempool/rte_mempool.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/librte_mempool/rte_mempool.c b/lib/librte_mempool/rte_mempool.c index f8781e1..01668c1 100644 --- a/lib/librte_mempool/rte_mempool.c +++ b/lib/librte_mempool/rte_mempool.c @@ -327,15 +327,19 @@ rte_mempool_calc_obj_size(uint32_t elt_size, uint32_t flags, size_t rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz, uint32_t pg_shift) { - size_t n, pg_num, pg_sz, sz; + size_t n, pg_num, pg_sz; + size_t sz = 0; - pg_sz = (size_t)1 << pg_shift; + if (elt_sz > 0) { + pg_sz = (size_t)1 << pg_shift; + n = pg_sz / elt_sz; - if ((n = pg_sz / elt_sz) > 0) { - pg_num = (elt_num + n - 1) / n; - sz = pg_num << pg_shift; - } else { - sz = RTE_ALIGN_CEIL(elt_sz, pg_sz) * elt_num; + if (n > 0) { + pg_num = (elt_num + n - 1) / n; + sz = pg_num << pg_shift; + } else { + sz = RTE_ALIGN_CEIL(elt_sz, pg_sz) * elt_num; + } } return sz; -- 1.9.1