Skip to contentSkip to conversation

wifi: brcmfmac: Replace strlcat() with snprintf() in brcmf_fw_alloc_request()

PATCH In review Mariia Nikitash 4 participants 5 messages Oct 14 – Nov 18 Broadcom BRCM80211 IEEE802.11 wireless drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wifi: brcmfmac: Replace strlcat() with snprintf() in brcmf_fw_alloc_request()

From: Bill Wendling <[email protected]>

brcmf_fw_alloc_request() constructs firmware file paths by chaining strscpy() and multiple strlcat() calls for the module parameter path, separator, base name, and extension.

In preparation for removing the deprecated strlcat() API[1], format the complete firmware path in a single snprintf() call.

---

This patch was originally part of Bill Wendling's strlcat() removal series [2], and I am shepherding it individually to the subsystem maintainers.

No code changes from the original posting [3].

[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

 .../broadcom/brcm80211/brcmfmac/firmware.c    | 21 +++++++------------
 1 file changed, 8 insertions(+), 13 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +8 -13
index 22ff326f1924..2f74a952599e 100644
@@ -845,22 +845,17 @@ brcmf_fw_alloc_request(u32 chip, u32 chiprev,
 	fwreq->n_items = n_fwnames;
 
 	for (j = 0; j < n_fwnames; j++) {
-		fwreq->items[j].path = fwnames[j].path;
-		fwnames[j].path[0] = '\0';
 		/* check if firmware path is provided by module parameter */
 		if (brcmf_mp_global.firmware_path[0] != '\0') {
-			strscpy(fwnames[j].path, mp_path,
-				BRCMF_FW_NAME_LEN);
-
-			if (end != '/') {
-				strlcat(fwnames[j].path, "/",
-					BRCMF_FW_NAME_LEN);
-			}
+			snprintf(fwnames[j].path, BRCMF_FW_NAME_LEN, "%s%s%s%s",
+				 mp_path, (end == '/') ? "" : "/",
+				 mapping_table[i].fw_base,
+				 fwnames[j].extension);
+		} else {
+			snprintf(fwnames[j].path, BRCMF_FW_NAME_LEN, "%s%s",
+				 mapping_table[i].fw_base,
+				 fwnames[j].extension);
 		}
-		strlcat(fwnames[j].path, mapping_table[i].fw_base,
-			BRCMF_FW_NAME_LEN);
-		strlcat(fwnames[j].path, fwnames[j].extension,
-			BRCMF_FW_NAME_LEN);
 		fwreq->items[j].path = fwnames[j].path;
 	}
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 33 lines · Mariia Nikitash

From: Bill Wendling <[email protected]>

brcmf_fw_alloc_request() constructs firmware file paths by chaining strscpy() and multiple strlcat() calls for the module parameter path, separator, base name, and extension.

In preparation for removing the deprecated strlcat() API[1], format the complete firmware path in a single snprintf() call.

---

This patch was originally part of Bill Wendling's strlcat() removal series [2], and I am shepherding it individually to the subsystem maintainers.

No code changes from the original posting [3].

[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

 .../broadcom/brcm80211/brcmfmac/firmware.c    | 21 +++++++------------
 1 file changed, 8 insertions(+), 13 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +0 -0
index 22ff326f1924..2f74a952599e 100644

drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c brcmf_fw_alloc_request(u32 chip, u32 chiprev, · line 848

Quoted 4 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +0 -1
@@ -845,22 +845,17 @@ brcmf_fw_alloc_request(u32 chip, u32 chiprev,
 	fwreq->n_items = n_fwnames;
 
 	for (j = 0; j < n_fwnames; j++) {
-		fwreq->items[j].path = fwnames[j].path;

yes, this is redundant to the final assignment.

drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c brcmf_fw_alloc_request(u32 chip, u32 chiprev, · line 849

Quoted 1 line
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +0 -1
-		fwnames[j].path[0] = '\0';

this pre-truncation isn't needed either.

(Maybe this code used to have a fast-exit path that left stuff unwritten?)

drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c brcmf_fw_alloc_request(u32 chip, u32 chiprev, · line 849

Quoted 2 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +0 -0
 		/* check if firmware path is provided by module parameter */
 		if (brcmf_mp_global.firmware_path[0] != '\0') {

This check is confusing, I would expect mp_path to be tested instead.

Quoted 6 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +0 -6
-			strscpy(fwnames[j].path, mp_path,
-				BRCMF_FW_NAME_LEN);
-
-			if (end != '/') {
-				strlcat(fwnames[j].path, "/",
-					BRCMF_FW_NAME_LEN);

drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c brcmf_fw_alloc_request(u32 chip, u32 chiprev, · line 858

Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +8 -1
-			}
+			snprintf(fwnames[j].path, BRCMF_FW_NAME_LEN, "%s%s%s%s",
+				 mp_path, (end == '/') ? "" : "/",
+				 mapping_table[i].fw_base,
+				 fwnames[j].extension);
+		} else {
+			snprintf(fwnames[j].path, BRCMF_FW_NAME_LEN, "%s%s",
+				 mapping_table[i].fw_base,
+				 fwnames[j].extension);
 		}

I don't see any reason not to fully collapse this, as the test is just examining mp_path and doing end checking:

		snprintf(fwnames[j].path, BRCMF_FW_NAME_LEN, "%s%s%s%s",
			 mp_path ?: "",
			 mp_path && end != '/' ? "/" : "",
			 mapping_table[i].fw_base,
			 fwnames[j].extension);
Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +0 -4
-		strlcat(fwnames[j].path, mapping_table[i].fw_base,
-			BRCMF_FW_NAME_LEN);
-		strlcat(fwnames[j].path, fwnames[j].extension,
-			BRCMF_FW_NAME_LEN);
 		fwreq->items[j].path = fwnames[j].path;
 	}
 
Signature · 1 line
2.43.0

-Kees

Signature · 1 line
Kees Cook
maintainer
#2 lore raw
In reply to Mariia Nikitash
Quoted 6 lines · Mariia Nikitash

From: Bill Wendling <[email protected]>

brcmf_fw_alloc_request() constructs firmware file paths by chaining strscpy() and multiple strlcat() calls for the module parameter path, separator, base name, and extension.

Quoted 6 lines

In preparation for removing the deprecated strlcat() API[1], format the complete firmware path in a single snprintf() call.

Is this Cc: line necessary? Can you clarify its purpose? No need to clarify in the commit message. Just an email response will do.

The Assisted-by: tag shows this change was produced with an LLM. Please follow the guidelines in Documentation/process/generated-content.rst so it is clear what was tool generated and what tool and/or LLM was used.

[...]

Quoted 17 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +5 -9
 	for (j = 0; j < n_fwnames; j++) {
-		fwreq->items[j].path = fwnames[j].path;
-		fwnames[j].path[0] = '\0';
 		/* check if firmware path is provided by module parameter */
 		if (brcmf_mp_global.firmware_path[0] != '\0') {
-			strscpy(fwnames[j].path, mp_path,
-				BRCMF_FW_NAME_LEN);
-
-			if (end != '/') {
-				strlcat(fwnames[j].path, "/",
-					BRCMF_FW_NAME_LEN);
-			}
+			snprintf(fwnames[j].path, BRCMF_FW_NAME_LEN, "%s%s%s%s",
+				 mp_path, (end == '/') ? "" : "/",
+				 mapping_table[i].fw_base,
+				 fwnames[j].extension);
+		} else {

drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c brcmf_fw_alloc_request(u32 chip, u32 chiprev, · line 860

Quoted 10 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/firmware.c +3 -4
+			snprintf(fwnames[j].path, BRCMF_FW_NAME_LEN, "%s%s",
+				 mapping_table[i].fw_base,
+				 fwnames[j].extension);
 		}
-		strlcat(fwnames[j].path, mapping_table[i].fw_base,
-			BRCMF_FW_NAME_LEN);
-		strlcat(fwnames[j].path, fwnames[j].extension,
-			BRCMF_FW_NAME_LEN);
 		fwreq->items[j].path = fwnames[j].path;
 	}

Please address Kees' comment and collapse this into a single snprintf(). Note mp_path points to the firmware_path array so it can never be NULL; an empty mp_path simply prints nothing, so something like the following would do:

	snprintf(fwnames[j].path, BRCMF_FW_NAME_LEN, "%s%s%s%s", mp_path,
		 (mp_path_len && end != '/') ? "/" : "",
		 mapping_table[i].fw_base, fwnames[j].extension);

Regards,
Arend

#3 lore raw
Quoted 3 lines

The Assisted-by: tag shows this change was produced with an LLM. Please follow the guidelines in Documentation/process/generated-content.rst so it is clear what was tool generated and what tool and/or LLM was used.

FYI. The Assisted-by is removed by:

1e035297ffda ("docs: remove Assisted-by references")

maintainer
#4 lore raw
Quoted 7 lines · Ping-Ke Shih
Quoted 3 lines

The Assisted-by: tag shows this change was produced with an LLM. Please follow the guidelines in Documentation/process/generated-content.rst so it is clear what was tool generated and what tool and/or LLM was used.

FYI. The Assisted-by is removed by:

1e035297ffda ("docs: remove Assisted-by references")

Right. It is however appreciated to be transparent about the use of generative tools in commit message and/or change log.

Regards,
Arend