[dpdk-dev,v3,18/24] rte_ether.h: explicit cast avoiding truncation warning
Checks
Commit Message
/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
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.
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
@@ -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 */
}