examples/kni: clear warning about discarding const qualifier

Message ID 20220531081304.229902-1-ke1x.zhang@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Andrew Rybchenko
Headers
Series examples/kni: clear warning about discarding const qualifier |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS
ci/iol-aarch64-unit-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-abi-testing warning Testing issues
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS

Commit Message

Zhang, Ke1X May 31, 2022, 8:13 a.m. UTC
  The warning info:
warning: passing argument 1 of ‘memcpy’ discards ‘const’
qualifier from pointer target type

Compulsory type conversion to clear compile warning.

Signed-off-by: Ke Zhang <ke1x.zhang@intel.com>
---
 kernel/linux/kni/kni_misc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Bruce Richardson May 31, 2022, 9:12 a.m. UTC | #1
On Tue, May 31, 2022 at 08:13:04AM +0000, Ke Zhang wrote:
> The warning info:
> warning: passing argument 1 of ‘memcpy’ discards ‘const’
> qualifier from pointer target type
> 
> Compulsory type conversion to clear compile warning.
> 
> Signed-off-by: Ke Zhang <ke1x.zhang@intel.com>
> ---
>  kernel/linux/kni/kni_misc.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/linux/kni/kni_misc.c b/kernel/linux/kni/kni_misc.c
> index 780187d8bf..6f9dab4732 100644
> --- a/kernel/linux/kni/kni_misc.c
> +++ b/kernel/linux/kni/kni_misc.c
> @@ -403,10 +403,10 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
>  
>  	/* if user has provided a valid mac address */
>  	if (is_valid_ether_addr(dev_info.mac_addr))
> -		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
> +		memcpy((unsigned char *)net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
>  	else
>  		/* Generate random MAC address. */
> -		eth_random_addr(net_dev->dev_addr);
> +		eth_random_addr((uint8_t *)net_dev->dev_addr);
>  
>  	if (dev_info.mtu)
>  		net_dev->mtu = dev_info.mtu;

+Stephen H on CC, for his advice

This fix seems wrong to do. Given that it's a pointer to const char* rather
than an actual array in the structure, is a better fix not to point the
pointer to a new area of memory rather than trying to overwrite the old
one?

/Bruce
  
Ferruh Yigit May 31, 2022, 9:22 a.m. UTC | #2
On 5/31/2022 10:12 AM, Bruce Richardson wrote:
> [CAUTION: External Email]
> 
> On Tue, May 31, 2022 at 08:13:04AM +0000, Ke Zhang wrote:
>> The warning info:
>> warning: passing argument 1 of ‘memcpy’ discards ‘const’
>> qualifier from pointer target type
>>
>> Compulsory type conversion to clear compile warning.
>>
>> Signed-off-by: Ke Zhang <ke1x.zhang@intel.com>
>> ---
>>   kernel/linux/kni/kni_misc.c | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/kernel/linux/kni/kni_misc.c b/kernel/linux/kni/kni_misc.c
>> index 780187d8bf..6f9dab4732 100644
>> --- a/kernel/linux/kni/kni_misc.c
>> +++ b/kernel/linux/kni/kni_misc.c
>> @@ -403,10 +403,10 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,
>>
>>        /* if user has provided a valid mac address */
>>        if (is_valid_ether_addr(dev_info.mac_addr))
>> -             memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
>> +             memcpy((unsigned char *)net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
>>        else
>>                /* Generate random MAC address. */
>> -             eth_random_addr(net_dev->dev_addr);
>> +             eth_random_addr((uint8_t *)net_dev->dev_addr);
>>
>>        if (dev_info.mtu)
>>                net_dev->mtu = dev_info.mtu;
> 
> +Stephen H on CC, for his advice
> 
> This fix seems wrong to do. Given that it's a pointer to const char* rather
> than an actual array in the structure, is a better fix not to point the
> pointer to a new area of memory rather than trying to overwrite the old
> one?
> 

Agree that this is not proper fix.

Variable seems done const intentionally to prevent using it directly,
there are new helper functions like 'eth_hw_addr_set()', 
'eth_hw_addr_random()', .. to use with newer kernel versions.
  

Patch

diff --git a/kernel/linux/kni/kni_misc.c b/kernel/linux/kni/kni_misc.c
index 780187d8bf..6f9dab4732 100644
--- a/kernel/linux/kni/kni_misc.c
+++ b/kernel/linux/kni/kni_misc.c
@@ -403,10 +403,10 @@  kni_ioctl_create(struct net *net, uint32_t ioctl_num,
 
 	/* if user has provided a valid mac address */
 	if (is_valid_ether_addr(dev_info.mac_addr))
-		memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
+		memcpy((unsigned char *)net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN);
 	else
 		/* Generate random MAC address. */
-		eth_random_addr(net_dev->dev_addr);
+		eth_random_addr((uint8_t *)net_dev->dev_addr);
 
 	if (dev_info.mtu)
 		net_dev->mtu = dev_info.mtu;