[PATCH net] net: vlan: fix format-truncation warnings in register_vlan_device

jiang.peng9@zte.com.cn posted 1 patch 6 months ago
net/8021q/vlan.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
[PATCH net] net: vlan: fix format-truncation warnings in register_vlan_device
Posted by jiang.peng9@zte.com.cn 6 months ago
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
Re: [PATCH net] net: vlan: fix format-truncation warnings in? register_vlan_device
Posted by Simon Horman 6 months ago
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


...
Re: [PATCH net] net: vlan: fix format-truncation warnings in? register_vlan_device
Posted by jiang.peng9@zte.com.cn 6 months ago
> 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