DPDK patches and discussions
 help / color / mirror / Atom feed
From: Slawomir Mrozowicz <slawomirx.mrozowicz@intel.com>
To: olivier.matz@6wind.com
Cc: dev@dpdk.org, Slawomir Mrozowicz <slawomirx.mrozowicz@intel.com>
Subject: [dpdk-dev] [PATCH] rte mempool: division or modulo by zero
Date: Thu, 12 May 2016 14:46:53 +0200	[thread overview]
Message-ID: <1463057213-4123-1-git-send-email-slawomirx.mrozowicz@intel.com> (raw)

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 <slawomirx.mrozowicz@intel.com>
---
 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

             reply	other threads:[~2016-05-12 12:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-12 12:46 Slawomir Mrozowicz [this message]
2016-05-16  9:22 ` Olivier Matz
2016-05-19 10:57   ` Mrozowicz, SlawomirX
2016-05-19 11:05     ` Olivier Matz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1463057213-4123-1-git-send-email-slawomirx.mrozowicz@intel.com \
    --to=slawomirx.mrozowicz@intel.com \
    --cc=dev@dpdk.org \
    --cc=olivier.matz@6wind.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).