* [PATCH net] net: vlan: fix format-truncation warnings in register_vlan_device
@ 2025-06-19 6:49 jiang.peng9
2025-06-19 12:35 ` [PATCH net] net: vlan: fix format-truncation warnings in? register_vlan_device Simon Horman
0 siblings, 1 reply; 3+ messages in thread
From: jiang.peng9 @ 2025-06-19 6:49 UTC (permalink / raw)
To: pabeni
Cc: davem, edumazet, kuba, horms, jiang.peng9, jiri, linux, oscmaes92,
netdev, linux-kernel, xu.xin16, yang.yang29
[-- Attachment #1.1.1: Type: text/plain, Size: 2720 bytes --]
From: Peng Jiang <jiang.peng9@zte.com.cn>
Building with W=1 triggers format-truncation warnings in the
register_vlan_device function when compiled with GCC 12.3.0.
These warnings occur due to the use of %i and %.4i format
specifiers with a buffer size that might be insufficient
for the formatted string, potentially causing truncation.
The original warning trace:
net/8021q/vlan.c:247:17: note: 'snprintf' output between 3 and 22 bytes into a destination of size 16
247 | snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
Signed-off-by: Peng Jiang <jiang.peng9@zte.com.cn>
---
net/8021q/vlan.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/net/8021q/vlan.c b/net/8021q/vlan.c
index 06908e37c3d9..f2c17035f625 100644
--- a/net/8021q/vlan.c
+++ b/net/8021q/vlan.c
@@ -217,7 +217,7 @@ static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
struct vlan_dev_priv *vlan;
struct net *net = dev_net(real_dev);
struct vlan_net *vn = net_generic(net, vlan_net_id);
- char name[IFNAMSIZ];
+ char name[IFNAMSIZ + 6]; /* plus extra 6 bytes for vlan_id */
int err;
if (vlan_id >= VLAN_VID_MASK)
@@ -232,26 +232,26 @@ static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
switch (vn->name_type) {
case VLAN_NAME_TYPE_RAW_PLUS_VID:
/* name will look like: eth1.0005 */
- snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
+ snprintf(name, sizeof(name), "%s.%.4i", real_dev->name, vlan_id);
break;
case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
/* Put our vlan.VID in the name.
* Name will look like: vlan5
*/
- snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
+ snprintf(name, sizeof(name), "vlan%i", vlan_id);
break;
case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
/* Put our vlan.VID in the name.
* Name will look like: eth0.5
*/
- snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
+ snprintf(name, sizeof(name), "%s.%i", real_dev->name, vlan_id);
break;
case VLAN_NAME_TYPE_PLUS_VID:
/* Put our vlan.VID in the name.
* Name will look like: vlan0005
*/
default:
- snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
+ snprintf(name, sizeof(name), "vlan%.4i", vlan_id);
}
new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name,
--
2.25.1
[-- Attachment #1.1.2: Type: text/html , Size: 4640 bytes --]
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH net] net: vlan: fix format-truncation warnings in? register_vlan_device
2025-06-19 6:49 [PATCH net] net: vlan: fix format-truncation warnings in register_vlan_device jiang.peng9
@ 2025-06-19 12:35 ` Simon Horman
2025-06-20 1:33 ` jiang.peng9
0 siblings, 1 reply; 3+ messages in thread
From: Simon Horman @ 2025-06-19 12:35 UTC (permalink / raw)
To: jiang.peng9
Cc: pabeni, davem, edumazet, kuba, jiri, linux, oscmaes92, netdev,
linux-kernel, xu.xin16, yang.yang29
On Thu, Jun 19, 2025 at 02:49:34PM +0800, jiang.peng9@zte.com.cn wrote:
> From: Peng Jiang <jiang.peng9@zte.com.cn>
>
> Building with W=1 triggers format-truncation warnings in the
> register_vlan_device function when compiled with GCC 12.3.0.
> These warnings occur due to the use of %i and %.4i format
> specifiers with a buffer size that might be insufficient
> for the formatted string, potentially causing truncation.
>
> The original warning trace:
> net/8021q/vlan.c:247:17: note: 'snprintf' output between 3 and 22 bytes into a destination of size 16
> 247 | snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
>
> Signed-off-by: Peng Jiang <jiang.peng9@zte.com.cn>
Hi Peng Jiang,
name is passed to alloc_netdev(). Which is a wrapper around alloc_netdev_mqs()
which includes the following check:
BUG_ON(strlen(name) >= sizeof(dev->name));
And the size of dev->name is IFNAMSIZ.
So while I am very pleased to see efforts to address format-truncation
warning - indeed I have made efforts elsewhere to this end myself - I don't
think we can solve this problem the way you propose.
Also, I suspect any work in this area will not be a bug fix, and
thus more appropriate to target at net-next rather than net.
Subject; [PATCH net-next]
And please make sure patches for net or next-next apply against
their target tree: this patch applies to cleanly to neither.
For more information on process for networking patches please see
https://docs.kernel.org/process/maintainer-netdev.html
--
pw-bot: changes-requested
...
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net] net: vlan: fix format-truncation warnings in? register_vlan_device
2025-06-19 12:35 ` [PATCH net] net: vlan: fix format-truncation warnings in? register_vlan_device Simon Horman
@ 2025-06-20 1:33 ` jiang.peng9
0 siblings, 0 replies; 3+ messages in thread
From: jiang.peng9 @ 2025-06-20 1:33 UTC (permalink / raw)
To: horms
Cc: pabeni, davem, edumazet, kuba, jiri, linux, oscmaes92, netdev,
linux-kernel, xu.xin16, yang.yang29
> name is passed to alloc_netdev(). Which is a wrapper around alloc_netdev_mqs()
> which includes the following check:
>
> BUG_ON(strlen(name) >= sizeof(dev->name));
>
> And the size of dev->name is IFNAMSIZ.
>
> So while I am very pleased to see efforts to address format-truncation
> warning - indeed I have made efforts elsewhere to this end myself - I don't
> think we can solve this problem the way you propose.
Thanks for pointing this out! After checking the code again, you're absolutely right - my proposed change could actually cause issues with alloc_netdev_mqs() since the BUG_ON check explicitly enforces the IFNAMSIZ limit.
It's unfortunate that we can't solve the warning this way, but I really appreciate you taking the time to explain the situation clearly. Your patience and attention to detail here are super helpful!
> Also, I suspect any work in this area will not be a bug fix, and
> thus more appropriate to target at net-next rather than net.
>
> Subject; [PATCH net-next]
>
> And please make sure patches for net or next-next apply against
> their target tree: this patch applies to cleanly to neither.
>
> For more information on process for networking patches please see
> https://docs.kernel.org/process/maintainer-netdev.html
Got it! Thanks again for your guidance on this - I really appreciate you taking the time to explain both the technical details and the proper submission process.
Best regards
Peng
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-06-20 1:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-19 6:49 [PATCH net] net: vlan: fix format-truncation warnings in register_vlan_device jiang.peng9
2025-06-19 12:35 ` [PATCH net] net: vlan: fix format-truncation warnings in? register_vlan_device Simon Horman
2025-06-20 1:33 ` jiang.peng9
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).