Netdev List
 help / color / mirror / Atom feed
From: david.laight.linux@gmail.com
To: Kees Cook <kees@kernel.org>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org
Cc: Arnd Bergmann <arnd@kernel.org>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>,
	Michael Chan <mchan@broadcom.com>,
	Paolo Abeni <pabeni@redhat.com>,
	Pavan Chebbi <pavan.chebbi@broadcom.com>,
	David Laight <david.laight.linux@gmail.com>
Subject: [PATCH next] Rewrite tg3_bus_string() to avoid temporary string
Date: Mon,  8 Jun 2026 10:55:07 +0100	[thread overview]
Message-ID: <20260608095523.2606-23-david.laight.linux@gmail.com> (raw)

From: David Laight <david.laight.linux@gmail.com>

Rather than build (eg) "PCIX:33MHz:32-bit" in a temporary buffer,
return the addresses of the three constituent components to be used
in the final snprintf() call.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
This is one of a group of patches that remove potentially unbounded
strcpy() calls.

They are mostly replaced by strscpy() or, when strlen() has just been
called, with memcpy() (usually including the '\0').

Calls with copy string literals into arrays are left unchanged.
They are safe and easily detected as such.

The changes were made by getting the compiler to detect the calls and
then fixing the code by hand.

Note that all the changes are only compile tested.

Some Makefiles were changed to allow files to contain strcpy().
As well as 'difficult to fix' files, this included 'show' functions
as they really need to use sysfs_emit() or seq_printf().

All the patches are being sent individually to avoid very long cc lists.
Apologies for the terse commit messages and likely unexpected tags.
(There are about 100 patches in total.)

 drivers/net/ethernet/broadcom/tg3.c | 38 +++++++++++++++--------------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/drivers/net/ethernet/broadcom/tg3.c b/drivers/net/ethernet/broadcom/tg3.c
index 73a4b569b03e..08bd5a074be8 100644
--- a/drivers/net/ethernet/broadcom/tg3.c
+++ b/drivers/net/ethernet/broadcom/tg3.c
@@ -17610,40 +17610,41 @@ static char *tg3_phy_string(struct tg3 *tp)
 	}
 }
 
-static char *tg3_bus_string(struct tg3 *tp, char *str)
+static void tg3_bus_string(struct tg3 *tp, const char **str_a, const char **str_b, const char **str_c)
 {
 	if (tg3_flag(tp, PCI_EXPRESS)) {
-		strcpy(str, "PCI Express");
-		return str;
+		*str_a =  "PCI Express";
+		*str_b = "";
+		*str_c = "";
+		return;
 	} else if (tg3_flag(tp, PCIX_MODE)) {
 		u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
 
-		strcpy(str, "PCIX:");
+		*str_a =  "PCIX:";
 
 		if ((clock_ctrl == 7) ||
 		    ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
 		     GRC_MISC_CFG_BOARD_ID_5704CIOBE))
-			strcat(str, "133MHz");
+			*str_b = "133MHz";
 		else if (clock_ctrl == 0)
-			strcat(str, "33MHz");
+			*str_b = "33MHz";
 		else if (clock_ctrl == 2)
-			strcat(str, "50MHz");
+			*str_b = "50MHz";
 		else if (clock_ctrl == 4)
-			strcat(str, "66MHz");
+			*str_b = "66MHz";
 		else if (clock_ctrl == 6)
-			strcat(str, "100MHz");
+			*str_b = "100MHz";
 	} else {
-		strcpy(str, "PCI:");
+		*str_a =  "PCI:";
 		if (tg3_flag(tp, PCI_HIGH_SPEED))
-			strcat(str, "66MHz");
+			*str_b = "66MHz";
 		else
-			strcat(str, "33MHz");
+			*str_b = "33MHz";
 	}
 	if (tg3_flag(tp, PCI_32BIT))
-		strcat(str, ":32-bit");
+		*str_c = ":32-bit";
 	else
-		strcat(str, ":64-bit");
-	return str;
+		*str_c = ":64-bit";
 }
 
 static void tg3_init_coal(struct tg3 *tp)
@@ -17684,7 +17685,7 @@ static int tg3_init_one(struct pci_dev *pdev,
 	struct tg3 *tp;
 	int i, err;
 	u32 sndmbx, rcvmbx, intmbx;
-	char str[40];
+	const char *str_a, *str_b, *str_c;
 	u64 dma_mask, persist_dma_mask;
 	netdev_features_t features = 0;
 	u8 addr[ETH_ALEN] __aligned(2);
@@ -18008,10 +18009,11 @@ static int tg3_init_one(struct pci_dev *pdev,
 			tp->ptp_clock = NULL;
 	}
 
-	netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
+	tg3_bus_string(tp, &str_a, &str_b, &str_c);
+	netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s%s%s) MAC address %pM\n",
 		    tp->board_part_number,
 		    tg3_chip_rev_id(tp),
-		    tg3_bus_string(tp, str),
+		    str_a, str_b, str_c,
 		    dev->dev_addr);
 
 	if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)) {
-- 
2.39.5


                 reply	other threads:[~2026-06-08  9:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260608095523.2606-23-david.laight.linux@gmail.com \
    --to=david.laight.linux@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=arnd@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kees@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchan@broadcom.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pavan.chebbi@broadcom.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox