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

From: jiang.peng9
Date: Thu Jun 19 2025 - 02:59:35 EST


From: Peng Jiang <jiang.peng9@xxxxxxxxxx>


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@xxxxxxxxxx>

---

 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