Skip to contentSkip to conversation

net/mana: avoid the use of variable length array

PATCH In review [email protected] 4 participants 4 messages Mar 5, 2025 – Mar 6, 2025 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/mana: avoid the use of variable length array

From: Long Li <[email protected]>

The pathname can be defined as name[MAX_PATH]. This makes the driver compilable using MSVC.

---
 drivers/net/mana/mana.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/mana/mana.c +2 -5
index c37c4e3444..d12dff6ce1 100644
@@ -36,11 +36,8 @@ static rte_spinlock_t mana_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
 
 /* Allocate a buffer on the stack and fill it with a printf format string. */
 #define MANA_MKSTR(name, ...) \
-	int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
-	char name[mkstr_size_##name + 1]; \
-	\
-	memset(name, 0, mkstr_size_##name + 1); \
-	snprintf(name, sizeof(name), "" __VA_ARGS__)
+	char name[PATH_MAX]; \
+	snprintf(name, PATH_MAX, "" __VA_ARGS__)
 
 int mana_logtype_driver;
 int mana_logtype_init;
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines

From: [email protected] [mailto:[email protected]]
Sent: Wednesday, 5 March 2025 01.38

From: Long Li <[email protected]>

The pathname can be defined as name[MAX_PATH]. This makes the driver compilable using MSVC.

The name is PATH_MAX, not MAX_PATH. It's correct in the code.

Quoted 14 lines
---
 drivers/net/mana/mana.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/mana/mana.c +0 -0
index c37c4e3444..d12dff6ce1 100644
@@ -36,11 +36,8 @@ static rte_spinlock_t mana_shared_data_lock =
 RTE_SPINLOCK_INITIALIZER;
 
 /* Allocate a buffer on the stack and fill it with a printf format
Quoted 2 lines

string. */
#define MANA_MKSTR(name, ...) \

The macro is no longer generic, but tied to a path name, so the macro's name and description should be updated accordingly. E.g.:

/* Allocate a path name buffer on the stack and fill it with a printf format string. */ #define MANA_MKPATHSTR(name, ...) \

Don't forget search-replace for uses of the macro. (It's being used 6 times in this file.)

Quoted 11 lines
drivers/net/mana/mana.c +2 -6
-	int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
-	char name[mkstr_size_##name + 1]; \
-	\
-	memset(name, 0, mkstr_size_##name + 1); \
-	snprintf(name, sizeof(name), "" __VA_ARGS__)
+	char name[PATH_MAX]; \
+	snprintf(name, PATH_MAX, "" __VA_ARGS__)
 
 int mana_logtype_driver;
 int mana_logtype_init;
--
Quoted 1 line

2.34.1

With suggested changes,

#2 lore raw
In reply to [email protected]
Quoted 30 lines · On Tue, Mar 04, 2025 at 04:37:32PM -0800, [email protected] wrote:

From: Long Li <[email protected]>

The pathname can be defined as name[MAX_PATH]. This makes the driver compilable using MSVC.

---
 drivers/net/mana/mana.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/mana/mana.c +2 -5
index c37c4e3444..d12dff6ce1 100644
@@ -36,11 +36,8 @@ static rte_spinlock_t mana_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
 
 /* Allocate a buffer on the stack and fill it with a printf format string. */
 #define MANA_MKSTR(name, ...) \
-	int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
-	char name[mkstr_size_##name + 1]; \
-	\
-	memset(name, 0, mkstr_size_##name + 1); \
-	snprintf(name, sizeof(name), "" __VA_ARGS__)
+	char name[PATH_MAX]; \
+	snprintf(name, PATH_MAX, "" __VA_ARGS__)
 
 int mana_logtype_driver;
 int mana_logtype_init;
Signature · 1 line
2.34.1

Did you try to remove the line below from mana/meson.build? That line prevents the compiler from complain about VLAs. If the driver is VLA-free after this fix it would be great if the compiler was allowed to complain about VLAs (default in DPDK project).

If the code still compiles without this line then it should be safe to remove it:

cflags += no_wvla_cflag
--
Andre Muezerie

#3 lore raw
Quoted 40 lines

Subject: Re: [PATCH] net/mana: avoid the use of variable length array

Quoted 28 lines · On Tue, Mar 04, 2025 at 04:37:32PM -0800, [email protected] wrote:

From: Long Li <[email protected]>

The pathname can be defined as name[MAX_PATH]. This makes the driver compilable using MSVC.

---
 drivers/net/mana/mana.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/net/mana/mana.c index renamed from drivers/net/mana/mana.c +0 -0

c37c4e3444..d12dff6ce1 100644

drivers/net/mana/mana.c +2 -5
@@ -36,11 +36,8 @@ static rte_spinlock_t mana_shared_data_lock =
 RTE_SPINLOCK_INITIALIZER;
 
 /* Allocate a buffer on the stack and fill it with a printf format
 string. */  #define MANA_MKSTR(name, ...) \
-	int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
-	char name[mkstr_size_##name + 1]; \
-	\
-	memset(name, 0, mkstr_size_##name + 1); \
-	snprintf(name, sizeof(name), "" __VA_ARGS__)
+	char name[PATH_MAX]; \
+	snprintf(name, PATH_MAX, "" __VA_ARGS__)
 
 int mana_logtype_driver;
Quoted 3 lines

int mana_logtype_init;
--
2.34.1

Did you try to remove the line below from mana/meson.build?
That line prevents the compiler from complain about VLAs. If the driver is VLA- free after this fix it would be great if the compiler was allowed to complain about VLAs (default in DPDK project).

Quoted 5 lines

If the code still compiles without this line then it should be safe to remove it:

cflags += no_wvla_cflag
--
Andre Muezerie

I tried, it gives warnings on other two places using VLA:

[1688/3164] Compiling C object drivers/libtmp_rte_net_mana.a.p/net_mana_mr.c.o ../drivers/net/mana/mr.c: In function 'mana_new_pmd_mr':
../drivers/net/mana/mr.c:41:16: warning: ISO C90 forbids variable length array 'ranges' [-Wvla]

   41 |         struct mana_range ranges[pool->nb_mem_chunks];
      |                ^~~~~~~~~~
[1710/3164] Compiling C object drivers/libtmp_rte_net_mana.a.p/net_mana_rx.c.o
../drivers/net/mana/rx.c: In function 'mana_start_rx_queues':
../drivers/net/mana/rx.c:244:16: warning: ISO C90 forbids variable length array 'ind_tbl' [-Wvla]
  244 |         struct ibv_wq *ind_tbl[priv->num_queues];
      |                ^~~~~~

I'll fix them up and send a patch.