[dpdk-dev,v3,18/24] rte_ether.h: explicit cast avoiding truncation warning

Message ID 152609041278.121661.12065395526931802948.stgit@localhost.localdomain (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Andy Green May 12, 2018, 2 a.m. UTC
  /projects/lagopus/src/dpdk/build/include/rte_ether.h:213:13:
warning: conversion from 'int' to 'uint8_t'
{aka 'unsigned char'} may change value [-Wconversion]
  addr[0] &= ~ETHER_GROUP_ADDR;
/* clear multicast bit */

Signed-off-by: Andy Green <andy@warmcat.com>
---
 lib/librte_net/rte_ether.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Thomas Monjalon May 13, 2018, 5:02 p.m. UTC | #1
12/05/2018 04:00, Andy Green:
> /projects/lagopus/src/dpdk/build/include/rte_ether.h:213:13:
> warning: conversion from 'int' to 'uint8_t'
> {aka 'unsigned char'} may change value [-Wconversion]
>   addr[0] &= ~ETHER_GROUP_ADDR;
> /* clear multicast bit */
[..]
>  	rte_memcpy(addr, p, ETHER_ADDR_LEN);
> -	addr[0] &= ~ETHER_GROUP_ADDR;       /* clear multicast bit */
> +	addr[0] &= (uint8_t)~ETHER_GROUP_ADDR;       /* clear multicast bit */
>  	addr[0] |= ETHER_LOCAL_ADMIN_ADDR;  /* set local assignment bit */

ETHER_GROUP_ADDR and ETHER_LOCAL_ADMIN_ADDR are defined macros,
they have no type, so I don't understand the need for casting.
And I don't understand why it is not needed for ETHER_LOCAL_ADMIN_ADDR.
  
Andy Green May 14, 2018, 12:05 a.m. UTC | #2
On 05/14/2018 01:02 AM, Thomas Monjalon wrote:
> 12/05/2018 04:00, Andy Green:
>> /projects/lagopus/src/dpdk/build/include/rte_ether.h:213:13:
>> warning: conversion from 'int' to 'uint8_t'
>> {aka 'unsigned char'} may change value [-Wconversion]
>>    addr[0] &= ~ETHER_GROUP_ADDR;
>> /* clear multicast bit */
> [..]
>>   	rte_memcpy(addr, p, ETHER_ADDR_LEN);
>> -	addr[0] &= ~ETHER_GROUP_ADDR;       /* clear multicast bit */
>> +	addr[0] &= (uint8_t)~ETHER_GROUP_ADDR;       /* clear multicast bit */
>>   	addr[0] |= ETHER_LOCAL_ADMIN_ADDR;  /* set local assignment bit */
> 
> ETHER_GROUP_ADDR and ETHER_LOCAL_ADMIN_ADDR are defined macros,
> they have no type, so I don't understand the need for casting.
> And I don't understand why it is not needed for ETHER_LOCAL_ADMIN_ADDR.

Both of those manifest constants are 0x1.

But ~ETHER_GROUP_ADDR is a "big number" in an int.

The compiler notices a definite truncation if you try to put 0xfffffffe 
in a uint8_t and complains.

If you try to put 0x01 in a uint8_t, the compiler feels it was OK.

-Andy
  

Patch

diff --git a/lib/librte_net/rte_ether.h b/lib/librte_net/rte_ether.h
index 95d0a533f..01d57f0ae 100644
--- a/lib/librte_net/rte_ether.h
+++ b/lib/librte_net/rte_ether.h
@@ -210,7 +210,7 @@  static inline void eth_random_addr(uint8_t *addr)
 	uint8_t *p = (uint8_t *)&rand;
 
 	rte_memcpy(addr, p, ETHER_ADDR_LEN);
-	addr[0] &= ~ETHER_GROUP_ADDR;       /* clear multicast bit */
+	addr[0] &= (uint8_t)~ETHER_GROUP_ADDR;       /* clear multicast bit */
 	addr[0] |= ETHER_LOCAL_ADMIN_ADDR;  /* set local assignment bit */
 }