Skip to contentSkip to conversation

net: sfc: avoid format string warning

PATCH Superseded Arnd Bergmann 4 participants 6 messages Mar 20 – Mar 25

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: sfc: avoid format string warning

From: Arnd Bergmann <[email protected]>

Three sfc drivers contain the same *_fill_test() function that takes a format string argument that gets passed down to snprintf(), producing a warning with clang-22:

drivers/net/ethernet/sfc/falcon/ethtool.c:227:60: error: diagnostic behavior may be improved by adding
the 'format(printf, 7, 8)' attribute to the declaration of 'ef4_fill_test' [-Werror,-Wmissing-format-attribute]
210 | snprintf(test_str, sizeof(test_str), test_format, test_id);
| ^
drivers/net/ethernet/sfc/falcon/ethtool.c:210:13: note: 'ef4_fill_test' declared here

Rework these to take a varargs based test_format that allows better type checking and avoids this warning.

Fixes 3273c2e8c66a ("[netdrvr] sfc: sfc: Add self-test support")
---
 drivers/net/ethernet/sfc/ethtool_common.c     | 28 ++++++++++--------
 drivers/net/ethernet/sfc/falcon/ethtool.c     | 27 ++++++++++-------
 .../net/ethernet/sfc/siena/ethtool_common.c   | 29 +++++++++++--------
 3 files changed, 49 insertions(+), 35 deletions(-)
drivers/net/ethernet/sfc/ethtool_common.c +16 -12
index 54f8e4626568..162be23edbe5 100644
@@ -254,9 +254,10 @@ int efx_ethtool_set_pauseparam(struct net_device *net_dev,
  *
  * Fill in an individual self-test entry.
  */
-static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
-			  int *test, const char *unit_format, int unit_id,
-			  const char *test_format, const char *test_id)
+static void __printf(7, 8)
+efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
+	      int *test, const char *unit_format, int unit_id,
+	      const char *test_format, ...)
 {
 	char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
 
@@ -266,12 +267,15 @@ static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
 
 	/* Fill string, if applicable */
 	if (strings) {
+		va_list args;
 		if (strchr(unit_format, '%'))
 			snprintf(unit_str, sizeof(unit_str),
 				 unit_format, unit_id);
 		else
 			strcpy(unit_str, unit_format);
-		snprintf(test_str, sizeof(test_str), test_format, test_id);
+		va_start(args, test_format);
+		vsnprintf(test_str, sizeof(test_str), test_format, args);
+		va_end(args);
 		snprintf(strings + test_index * ETH_GSTRING_LEN,
 			 ETH_GSTRING_LEN,
 			 "%-6s %-24s", unit_str, test_str);
@@ -349,28 +353,28 @@ int efx_ethtool_fill_self_tests(struct efx_nic *efx,
 	enum efx_loopback_mode mode;
 
 	efx_fill_test(n++, strings, data, &tests->phy_alive,
-		      "phy", 0, "alive", NULL);
+		      "phy", 0, "alive");
 	efx_fill_test(n++, strings, data, &tests->nvram,
-		      "core", 0, "nvram", NULL);
+		      "core", 0, "nvram");
 	efx_fill_test(n++, strings, data, &tests->interrupt,
-		      "core", 0, "interrupt", NULL);
+		      "core", 0, "interrupt");
 
 	/* Event queues */
 	efx_for_each_channel(channel, efx) {
 		efx_fill_test(n++, strings, data,
 			      &tests->eventq_dma[channel->channel],
 			      EFX_CHANNEL_NAME(channel),
-			      "eventq.dma", NULL);
+			      "eventq.dma");
 		efx_fill_test(n++, strings, data,
 			      &tests->eventq_int[channel->channel],
 			      EFX_CHANNEL_NAME(channel),
-			      "eventq.int", NULL);
+			      "eventq.int");
 	}
 
 	efx_fill_test(n++, strings, data, &tests->memory,
-		      "core", 0, "memory", NULL);
+		      "core", 0, "memory");
 	efx_fill_test(n++, strings, data, &tests->registers,
-		      "core", 0, "registers", NULL);
+		      "core", 0, "registers");
 
 	for (i = 0; true; ++i) {
 		const char *name;
@@ -380,7 +384,7 @@ int efx_ethtool_fill_self_tests(struct efx_nic *efx,
 		if (name == NULL)
 			break;
 
-		efx_fill_test(n++, strings, data, &tests->phy_ext[i], "phy", 0, name, NULL);
+		efx_fill_test(n++, strings, data, &tests->phy_ext[i], "phy", 0, "%s", name);
 	}
 
 	/* Loopback tests */
drivers/net/ethernet/sfc/falcon/ethtool.c +16 -11
index 807c587eba3b..1ae8acb8fdc8 100644
@@ -207,9 +207,10 @@ static void ef4_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
  *
  * Fill in an individual self-test entry.
  */
-static void ef4_fill_test(unsigned int test_index, u8 *strings, u64 *data,
+static void __printf(7, 8)
+ ef4_fill_test(unsigned int test_index, u8 *strings, u64 *data,
 			  int *test, const char *unit_format, int unit_id,
-			  const char *test_format, const char *test_id)
+			  const char *test_format, ...)
 {
 	char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
 
@@ -219,12 +220,16 @@ static void ef4_fill_test(unsigned int test_index, u8 *strings, u64 *data,
 
 	/* Fill string, if applicable */
 	if (strings) {
+		va_list arg;
+
 		if (strchr(unit_format, '%'))
 			snprintf(unit_str, sizeof(unit_str),
 				 unit_format, unit_id);
 		else
 			strcpy(unit_str, unit_format);
-		snprintf(test_str, sizeof(test_str), test_format, test_id);
+		va_start(arg, test_format);
+		vsnprintf(test_str, sizeof(test_str), test_format, arg);
+		va_end(arg);
 		snprintf(strings + test_index * ETH_GSTRING_LEN,
 			 ETH_GSTRING_LEN,
 			 "%-6s %-24s", unit_str, test_str);
@@ -303,28 +308,28 @@ static int ef4_ethtool_fill_self_tests(struct ef4_nic *efx,
 	enum ef4_loopback_mode mode;
 
 	ef4_fill_test(n++, strings, data, &tests->phy_alive,
-		      "phy", 0, "alive", NULL);
+		      "phy", 0, "alive");
 	ef4_fill_test(n++, strings, data, &tests->nvram,
-		      "core", 0, "nvram", NULL);
+		      "core", 0, "nvram");
 	ef4_fill_test(n++, strings, data, &tests->interrupt,
-		      "core", 0, "interrupt", NULL);
+		      "core", 0, "interrupt");
 
 	/* Event queues */
 	ef4_for_each_channel(channel, efx) {
 		ef4_fill_test(n++, strings, data,
 			      &tests->eventq_dma[channel->channel],
 			      EF4_CHANNEL_NAME(channel),
-			      "eventq.dma", NULL);
+			      "eventq.dma");
 		ef4_fill_test(n++, strings, data,
 			      &tests->eventq_int[channel->channel],
 			      EF4_CHANNEL_NAME(channel),
-			      "eventq.int", NULL);
+			      "eventq.int");
 	}
 
 	ef4_fill_test(n++, strings, data, &tests->memory,
-		      "core", 0, "memory", NULL);
+		      "core", 0, "memory");
 	ef4_fill_test(n++, strings, data, &tests->registers,
-		      "core", 0, "registers", NULL);
+		      "core", 0, "registers");
 
 	if (efx->phy_op->run_tests != NULL) {
 		EF4_BUG_ON_PARANOID(efx->phy_op->test_name == NULL);
@@ -338,7 +343,7 @@ static int ef4_ethtool_fill_self_tests(struct ef4_nic *efx,
 				break;
 
 			ef4_fill_test(n++, strings, data, &tests->phy_ext[i],
-				      "phy", 0, name, NULL);
+				      "phy", 0, "%s", name);
 		}
 	}
 
drivers/net/ethernet/sfc/siena/ethtool_common.c +17 -12
index 76cbce2b9592..9a8e39c5a113 100644
@@ -206,9 +206,9 @@ int efx_siena_ethtool_set_pauseparam(struct net_device *net_dev,
  *
  * Fill in an individual self-test entry.
  */
-static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
-			  int *test, const char *unit_format, int unit_id,
-			  const char *test_format, const char *test_id)
+static void __printf(7, 8)
+efx_fill_test(unsigned int test_index, u8 *strings, u64 *data, int *test,
+	      const char *unit_format, int unit_id, const char *test_format, ...)
 {
 	char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
 
@@ -218,12 +218,16 @@ static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
 
 	/* Fill string, if applicable */
 	if (strings) {
+		va_list args;
+
 		if (strchr(unit_format, '%'))
 			snprintf(unit_str, sizeof(unit_str),
 				 unit_format, unit_id);
 		else
 			strcpy(unit_str, unit_format);
-		snprintf(test_str, sizeof(test_str), test_format, test_id);
+		va_start(args, test_format);
+		vsnprintf(test_str, sizeof(test_str), test_format, args);
+		va_end(args);
 		snprintf(strings + test_index * ETH_GSTRING_LEN,
 			 ETH_GSTRING_LEN,
 			 "%-6s %-24s", unit_str, test_str);
@@ -301,28 +305,28 @@ static int efx_ethtool_fill_self_tests(struct efx_nic *efx,
 	enum efx_loopback_mode mode;
 
 	efx_fill_test(n++, strings, data, &tests->phy_alive,
-		      "phy", 0, "alive", NULL);
+		      "phy", 0, "alive");
 	efx_fill_test(n++, strings, data, &tests->nvram,
-		      "core", 0, "nvram", NULL);
+		      "core", 0, "nvram");
 	efx_fill_test(n++, strings, data, &tests->interrupt,
-		      "core", 0, "interrupt", NULL);
+		      "core", 0, "interrupt");
 
 	/* Event queues */
 	efx_for_each_channel(channel, efx) {
 		efx_fill_test(n++, strings, data,
 			      &tests->eventq_dma[channel->channel],
 			      EFX_CHANNEL_NAME(channel),
-			      "eventq.dma", NULL);
+			      "eventq.dma");
 		efx_fill_test(n++, strings, data,
 			      &tests->eventq_int[channel->channel],
 			      EFX_CHANNEL_NAME(channel),
-			      "eventq.int", NULL);
+			      "eventq.int");
 	}
 
 	efx_fill_test(n++, strings, data, &tests->memory,
-		      "core", 0, "memory", NULL);
+		      "core", 0, "memory");
 	efx_fill_test(n++, strings, data, &tests->registers,
-		      "core", 0, "registers", NULL);
+		      "core", 0, "registers");
 
 	for (i = 0; true; ++i) {
 		const char *name;
@@ -332,7 +336,8 @@ static int efx_ethtool_fill_self_tests(struct efx_nic *efx,
 		if (name == NULL)
 			break;
 
-		efx_fill_test(n++, strings, data, &tests->phy_ext[i], "phy", 0, name, NULL);
+		efx_fill_test(n++, strings, data, &tests->phy_ext[i], "phy",
+			      0, "%s", name);
 	}
 
 	/* Loopback tests */
Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Three sfc drivers contain the same *_fill_test() function that takes a format string argument that gets passed down to snprintf(), producing a warning with clang-22:

drivers/net/ethernet/sfc/falcon/ethtool.c:227:60: error: diagnostic behavior may be improved by adding
the 'format(printf, 7, 8)' attribute to the declaration of 'ef4_fill_test' [-Werror,-Wmissing-format-attribute]
210 | snprintf(test_str, sizeof(test_str), test_format, test_id);
| ^
drivers/net/ethernet/sfc/falcon/ethtool.c:210:13: note: 'ef4_fill_test' declared here

Quoted 5 lines

Rework these to take a varargs based test_format that allows better type checking and avoids this warning.

Fixes 3273c2e8c66a ("[netdrvr] sfc: sfc: Add self-test support")

Nice rework!

Signature · 1 line
Kees Cook
maintainer
#2 lore raw
In reply to Arnd Bergmann
Quoted 12 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Three sfc drivers contain the same *_fill_test() function that takes a format string argument that gets passed down to snprintf(), producing a warning with clang-22:

drivers/net/ethernet/sfc/falcon/ethtool.c:227:60: error: diagnostic behavior may be improved by adding
the 'format(printf, 7, 8)' attribute to the declaration of 'ef4_fill_test' [-Werror,-Wmissing-format-attribute]
210 | snprintf(test_str, sizeof(test_str), test_format, test_id);
| ^
drivers/net/ethernet/sfc/falcon/ethtool.c:210:13: note: 'ef4_fill_test' declared here

Quoted 5 lines

Rework these to take a varargs based test_format that allows better type checking and avoids this warning.

Fixes 3273c2e8c66a ("[netdrvr] sfc: sfc: Add self-test support")

I'm not convinced this is an improvement. The function takes *two* printf
format strings (unit_format and test_format), and this only annotates and varargs-ifies one of them (and doesn't harden the unit_format, which has the considerably more worrying-looking "if it has a % in, assume it takes precisely one int argument").
So it may make things 'look' hardened enough to satisfy the compiler
warning, but that just hides the unsafety and might sucker future developers into thinking "oh, the compiler knows about the format strings so I don't need to check it by hand".
I'm not sure what a proper fix would look like — possibly a rewrite adding
a separate efx_fill_test_perq() function for the cases with a nontrivial unit_format, baking in the knowledge that the format is always "foo%d" for some constant string foo and thus we don't actually need to pass in an arbitrary format string.

drivers/net/ethernet/sfc/ethtool_common.c int efx_ethtool_set_pauseparam(struct net_device *net_dev, · line 258

Quoted 5 lines
drivers/net/ethernet/sfc/ethtool_common.c +2 -3
-static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
-			  int *test, const char *unit_format, int unit_id,
-			  const char *test_format, const char *test_id)
+static void __printf(7, 8)
+efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,

I don't like splitting the specifiers/return type from the function name
like this. It's not Linux style[1].
(From a quick poke around the tree, there seems to be less consensus on
where the __printf attribute goes — some put it on its own line before static, some would put it between static and void… sigh.)

-Ed

[1]: https://lore.kernel.org/all/[email protected]/T/#u

#3 lore raw
Quoted 16 lines · Edward Cree
Quoted 15 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Three sfc drivers contain the same *_fill_test() function that takes a format string argument that gets passed down to snprintf(), producing a warning with clang-22:

drivers/net/ethernet/sfc/falcon/ethtool.c:227:60: error: diagnostic behavior may be improved by adding
the 'format(printf, 7, 8)' attribute to the declaration of 'ef4_fill_test' [-Werror,-Wmissing-format-attribute]
210 | snprintf(test_str, sizeof(test_str), test_format, test_id);
| ^
drivers/net/ethernet/sfc/falcon/ethtool.c:210:13: note: 'ef4_fill_test' declared here

Rework these to take a varargs based test_format that allows better type checking and avoids this warning.

Quoted 8 lines
Quoted 2 lines
Fixes 3273c2e8c66a ("[netdrvr] sfc: sfc: Add self-test support")

I'm not convinced this is an improvement. The function takes *two* printf
format strings (unit_format and test_format), and this only annotates and varargs-ifies one of them (and doesn't harden the unit_format, which has the considerably more worrying-looking "if it has a % in, assume it takes precisely one int argument").

Right, I took a bit of a shortcut there. How about changing it so the other format string is just a name that gets joined with the number?

drivers/net/ethernet/sfc/falcon/ethtool.c +18 -18
index 1ae8acb8fdc8..a0064eadff41 100644
@@ -200,7 +200,7 @@ static void ef4_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
  * @strings:		Ethtool strings, or %NULL
  * @data:		Ethtool test results, or %NULL
  * @test:		Pointer to test result (used only if data != %NULL)
- * @unit_format:	Unit name format (e.g. "chan\%d")
+ * @unit_name:		Unit name format (e.g. "chan\%d")
  * @unit_id:		Unit id (e.g. 0 for "chan0")
  * @test_format:	Test name format (e.g. "loopback.\%s.tx.sent")
  * @test_id:		Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
@@ -208,9 +208,9 @@ static void ef4_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
  * Fill in an individual self-test entry.
  */
 static void __printf(7, 8)
- ef4_fill_test(unsigned int test_index, u8 *strings, u64 *data,
-			  int *test, const char *unit_format, int unit_id,
-			  const char *test_format, ...)
+ef4_fill_test(unsigned int test_index, u8 *strings, u64 *data,
+	      int *test, const char *unit_name, int unit_id,
+	      const char *test_format, ...)
 {
 	char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
 
@@ -222,11 +222,11 @@ static void __printf(7, 8)
 	if (strings) {
 		va_list arg;
 
-		if (strchr(unit_format, '%'))
+		if (unit_id >= 0)
 			snprintf(unit_str, sizeof(unit_str),
-				 unit_format, unit_id);
+				 "%s%d", unit_name, unit_id);
 		else
-			strcpy(unit_str, unit_format);
+			strcpy(unit_str, unit_name);
 		va_start(arg, test_format);
 		vsnprintf(test_str, sizeof(test_str), test_format, arg);
 		va_end(arg);
@@ -236,9 +236,9 @@ static void __printf(7, 8)
 	}
 }
 
-#define EF4_CHANNEL_NAME(_channel) "chan%d", _channel->channel
-#define EF4_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->queue
-#define EF4_RX_QUEUE_NAME(_rx_queue) "rxq%d", _rx_queue->queue
+#define EF4_CHANNEL_NAME(_channel) "chan", _channel->channel
+#define EF4_TX_QUEUE_NAME(_tx_queue) "txq", _tx_queue->queue
+#define EF4_RX_QUEUE_NAME(_rx_queue) "rxq", _rx_queue->queue
 #define EF4_LOOPBACK_NAME(_mode, _counter)			\
 	"loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, ef4_loopback_mode)
 
@@ -276,11 +276,11 @@ static int ef4_fill_loopback_test(struct ef4_nic *efx,
 	}
 	ef4_fill_test(test_index++, strings, data,
 		      &lb_tests->rx_good,
-		      "rx", 0,
+		      "rx", -1,
 		      EF4_LOOPBACK_NAME(mode, "rx_good"));
 	ef4_fill_test(test_index++, strings, data,
 		      &lb_tests->rx_bad,
-		      "rx", 0,
+		      "rx", -1,
 		      EF4_LOOPBACK_NAME(mode, "rx_bad"));
 
 	return test_index;
@@ -308,11 +308,11 @@ static int ef4_ethtool_fill_self_tests(struct ef4_nic *efx,
 	enum ef4_loopback_mode mode;
 
 	ef4_fill_test(n++, strings, data, &tests->phy_alive,
-		      "phy", 0, "alive");
+		      "phy", -1, "alive");
 	ef4_fill_test(n++, strings, data, &tests->nvram,
-		      "core", 0, "nvram");
+		      "core", -1, "nvram");
 	ef4_fill_test(n++, strings, data, &tests->interrupt,
-		      "core", 0, "interrupt");
+		      "core", -1, "interrupt");
 
 	/* Event queues */
 	ef4_for_each_channel(channel, efx) {
@@ -327,9 +327,9 @@ static int ef4_ethtool_fill_self_tests(struct ef4_nic *efx,
 	}
 
 	ef4_fill_test(n++, strings, data, &tests->memory,
-		      "core", 0, "memory");
+		      "core", -1, "memory");
 	ef4_fill_test(n++, strings, data, &tests->registers,
-		      "core", 0, "registers");
+		      "core", -1, "registers");
 
 	if (efx->phy_op->run_tests != NULL) {
 		EF4_BUG_ON_PARANOID(efx->phy_op->test_name == NULL);
@@ -343,7 +343,7 @@ static int ef4_ethtool_fill_self_tests(struct ef4_nic *efx,
 				break;
 
 			ef4_fill_test(n++, strings, data, &tests->phy_ext[i],
-				      "phy", 0, "%s", name);
+				      "phy", -1, "%s", name);
 		}
 	}
 
Quoted 7 lines

I don't like splitting the specifiers/return type from the function name
like this. It's not Linux style[1].
(From a quick poke around the tree, there seems to be less consensus on
where the __printf attribute goes — some put it on its own line before static, some would put it between static and void… sigh.)

[1]: https://lore.kernel.org/all/[email protected]/T/#u

There are not a lot of good options here, and splitting the line seems better than an overlong line to me. I don't really have a strong opinion on where the __printf attribute should go either, but I do see that after the return type is probably the least common, so I'll change that. How about having the specifiers on one line and
the type in front of the name? that seems faily common.

static __printf(7, 8)
void ef4_fill_test(unsigned int test_index, u8 *strings, u64 *data,

Arnd

maintainer
#4 lore raw
Quoted 3 lines · Arnd Bergmann

Right, I took a bit of a shortcut there. How about changing it so the other format string is just a name that gets joined with the number?

That would work, but all those -1s are a bit unsightly. Like I said,
I'd rather have two different efx_fill_test() functions, one which takes a unit_id and one which doesn't.
(Also you need to change the kdoc description of unit_name.)

Quoted 9 lines

There are not a lot of good options here, and splitting the line seems better than an overlong line to me. I don't really have a strong opinion on where the __printf attribute should go either, but I do see that after the return type is probably the least common, so I'll change that. How about having the specifiers on one line and
the type in front of the name? that seems faily common.

static __printf(7, 8)
void ef4_fill_test(unsigned int test_index, u8 *strings, u64 *data,

I'd rather just have
static void __printf(7, 8) ef4_fill_test(unsigned int test_index,

Admittedly that puts the ( further over so fewer params fit on each
line, but I definitely feel the function name should always appear on the first line of the definition.

-ed

#5 lore raw
Quoted 6 lines · Edward Cree

On 20/03/2026 20:48, Arnd Bergmann wrote:

That would work, but all those -1s are a bit unsightly. Like I said,
I'd rather have two different efx_fill_test() functions, one which takes a unit_id and one which doesn't.
(Also you need to change the kdoc description of unit_name.)

I sent a new version now, tried your suggestion but ended up with yet another variation where I preformat the string in the two cases (per driver) that actually use a nontrivial format. This ended up simpler both four the source and compiled form.

Quoted 7 lines
Quoted 7 lines

There are not a lot of good options here, and splitting the line seems better than an overlong line to me. I don't really have a strong opinion on where the __printf attribute should go either, but I do see that after the return type is probably the least common, so I'll change that. How about having the specifiers on one line and
the type in front of the name? that seems faily common.

Quoted 5 lines
Quoted 2 lines

static __printf(7, 8)
void ef4_fill_test(unsigned int test_index, u8 *strings, u64 *data,

I'd rather just have
static void __printf(7, 8) ef4_fill_test(unsigned int test_index,

Done.

Arnd