DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH] rte mempool: division or modulo by zero
@ 2016-05-12 12:46 Slawomir Mrozowicz
  2016-05-16  9:22 ` Olivier Matz
  0 siblings, 1 reply; 4+ messages in thread
From: Slawomir Mrozowicz @ 2016-05-12 12:46 UTC (permalink / raw)
  To: olivier.matz; +Cc: dev, Slawomir Mrozowicz

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [dpdk-dev] [PATCH] rte mempool: division or modulo by zero
  2016-05-12 12:46 [dpdk-dev] [PATCH] rte mempool: division or modulo by zero Slawomir Mrozowicz
@ 2016-05-16  9:22 ` Olivier Matz
  2016-05-19 10:57   ` Mrozowicz, SlawomirX
  0 siblings, 1 reply; 4+ messages in thread
From: Olivier Matz @ 2016-05-16  9:22 UTC (permalink / raw)
  To: Slawomir Mrozowicz; +Cc: dev

Hi Slawomir,

On 05/12/2016 02:46 PM, Slawomir Mrozowicz wrote:
> 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;
> 

I think it would be clearer (either for the patch and the code) to avoid
an additional indent, and do something like that:

	size_t
	rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
		uint32_t pg_shift)
	{
		if (elt_sz == 0)
			return 0;

		/* same code as before */

It will also facilitate the merge with
http://patchwork.dpdk.org/dev/patchwork/patch/12057/

Could you please submit a v2 with this logic?

Thanks,
Olivier

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [dpdk-dev] [PATCH] rte mempool: division or modulo by zero
  2016-05-16  9:22 ` Olivier Matz
@ 2016-05-19 10:57   ` Mrozowicz, SlawomirX
  2016-05-19 11:05     ` Olivier Matz
  0 siblings, 1 reply; 4+ messages in thread
From: Mrozowicz, SlawomirX @ 2016-05-19 10:57 UTC (permalink / raw)
  To: Olivier Matz; +Cc: dev

Hi Olivier,

I try to marge my change CID 13234 with your patch 12057.
Can you tell me  which is the base commit to apply the patch.
I think that I should apply your patches starting  from 12834.

Regards,
Slawomir


>-----Original Message-----
>From: Olivier Matz [mailto:olivier.matz@6wind.com]
>Sent: Monday, May 16, 2016 11:23 AM
>To: Mrozowicz, SlawomirX <slawomirx.mrozowicz@intel.com>
>Cc: dev@dpdk.org
>Subject: Re: [PATCH] rte mempool: division or modulo by zero
>
>Hi Slawomir,
>
>On 05/12/2016 02:46 PM, Slawomir Mrozowicz wrote:
>> 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;
>>
>
>I think it would be clearer (either for the patch and the code) to avoid an
>additional indent, and do something like that:
>
>	size_t
>	rte_mempool_xmem_size(uint32_t elt_num, size_t elt_sz,
>		uint32_t pg_shift)
>	{
>		if (elt_sz == 0)
>			return 0;
>
>		/* same code as before */
>
>It will also facilitate the merge with
>http://patchwork.dpdk.org/dev/patchwork/patch/12057/
>
>Could you please submit a v2 with this logic?
>
>Thanks,
>Olivier

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [dpdk-dev] [PATCH] rte mempool: division or modulo by zero
  2016-05-19 10:57   ` Mrozowicz, SlawomirX
@ 2016-05-19 11:05     ` Olivier Matz
  0 siblings, 0 replies; 4+ messages in thread
From: Olivier Matz @ 2016-05-19 11:05 UTC (permalink / raw)
  To: Mrozowicz, SlawomirX; +Cc: dev

Hi Slawomir,

On 05/19/2016 12:57 PM, Mrozowicz, SlawomirX wrote:
> Hi Olivier,
> 
> I try to marge my change CID 13234 with your patch 12057.
> Can you tell me  which is the base commit to apply the patch.
> I think that I should apply your patches starting  from 12834.
> 

Yes that's correct, the v3 patchset can be found here:
http://dpdk.org/ml/archives/dev/2016-May/039229.html

Regards,
Olivier

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-05-19 11:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-12 12:46 [dpdk-dev] [PATCH] rte mempool: division or modulo by zero Slawomir Mrozowicz
2016-05-16  9:22 ` Olivier Matz
2016-05-19 10:57   ` Mrozowicz, SlawomirX
2016-05-19 11:05     ` Olivier Matz

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).