examples/kni: clear warning about discarding const qualifier
PATCH 1/1 examples/kni: clear warning about discarding const qualifier
The warning info:
warning: passing argument 1 of ‘memcpy’ discards ‘const’ qualifier from pointer target type
Compulsory type conversion to clear compile warning.
--- kernel/linux/kni/kni_misc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/linux/kni/kni_misc.c +2 -2
@@ -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;
Signature · 1 line
2.25.1
Quoted 18 lines · Ke Zhang
The warning info:
warning: passing argument 1 of ‘memcpy’ discards ‘const’ qualifier from pointer target typeCompulsory type conversion to clear compile warning.
Signed-off-by Ke Zhang <[email protected]>--- kernel/linux/kni/kni_misc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/linux/kni/kni_misc.c +0
-0@@ -403,10 +403,10 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,/* if user has provided a valid mac address */
kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 412
Quoted 10 lines
kernel/linux/kni/kni_misc.c +2
-2if (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
Quoted 34 lines · Bruce Richardson
[CAUTION: External Email]
Quoted 18 lines · Ke Zhang
The warning info:
warning: passing argument 1 of ‘memcpy’ discards ‘const’ qualifier from pointer target typeCompulsory type conversion to clear compile warning.
Signed-off-by Ke Zhang <[email protected]>--- kernel/linux/kni/kni_misc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/linux/kni/kni_misc.c +0
-0@@ -403,10 +403,10 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num,/* if user has provided a valid mac address */ kernel/linux/kni/kni_misc.c kni_ioctl_create(struct net *net, uint32_t ioctl_num, · line 412
Quoted 10 lines
kernel/linux/kni/kni_misc.c +2
-2if (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
Quoted 5 lines
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 v3 1/1 kni: fix warning about discarding const qualifier
In reply to Ke ZhangThe warning info:
warning: passing argument 1 of ‘memcpy’ discards ‘const’ qualifier from pointer target type
Variable dev_addr is done const intentionally in v5.17 to prevent using it directly. See kernel series [1] for more information.
[1] https://lore.kernel.org/netdev/YZYAb4X%2FVQFy0iks@shredder/T/
--- kernel/linux/kni/compat.h | 4 ++++ kernel/linux/kni/kni_misc.c | 6 +++++- kernel/linux/kni/kni_net.c | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-)
kernel/linux/kni/compat.h +4 -0
@@ -141,3 +141,7 @@#if KERNEL_VERSION (5 ,9 ,0 ) >LINUX_VERSION_CODE #define HAVE_TSK_IN_GUP #endif + +#if KERNEL_VERSION (5 ,15 ,0 ) <=LINUX_VERSION_CODE +#define HAVE_ETH_HW_ADDR_SET +#endif
kernel/linux/kni/kni_misc.c +5 -1
@@ -403,10 +403,14 @@ 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)) +#ifdef HAVE_ETH_HW_ADDR_SET +eth_hw_addr_set (net_dev, dev_info.mac_addr); +#else memcpy (net_dev->dev_addr, dev_info.mac_addr,ETH_ALEN ); +#endif else/* Generate random MAC address. */ -eth_random_addr (net_dev->dev_addr); +eth_hw_addr_random (net_dev );if (dev_info.mtu) net_dev->mtu = dev_info.mtu;
kernel/linux/kni/kni_net.c +4 -1
@@ -779,8 +779,11 @@ kni_net_set_mac(struct net_device *netdev, void *p)return -EADDRNOTAVAIL ;memcpy (req.mac_addr, addr->sa_data, netdev->addr_len); +#ifdef HAVE_ETH_HW_ADDR_SET +eth_hw_addr_set (netdev, addr->sa_data); +#else memcpy (netdev->dev_addr, addr->sa_data, netdev->addr_len); - +#endif ret =kni_net_process_request (netdev, &req);return (ret ==0 ? req.result : ret);
Signature · 1 line
2.25.1