netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [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

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).