diff mbox series

[v3,7/9] brcmfmac: of: Use devm_kstrdup for board_type & check for errors

Message ID 20220117142919.207370-8-marcan@marcan.st
State Superseded
Headers show
Series misc brcmfmac fixes (M1/T2 series spin-off) | expand

Commit Message

Hector Martin Jan. 17, 2022, 2:29 p.m. UTC
This was missing a NULL check, and we can collapse the strlen/alloc/copy
into a devm_kstrdup().

Signed-off-by: Hector Martin <marcan@marcan.st>
---
 drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

Comments

Arend van Spriel Jan. 19, 2022, 12:35 p.m. UTC | #1
On 1/17/2022 3:29 PM, Hector Martin wrote:
> This was missing a NULL check, and we can collapse the strlen/alloc/copy
> into a devm_kstrdup().

Reviewed-by: Arend van Spriel <arend.vanspriel@broadcom.com>
> Signed-off-by: Hector Martin <marcan@marcan.st>
> ---
>   drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c
> index 513c7e6421b2..5dc1e942e9e7 100644
> --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c
> +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c
> @@ -79,8 +79,12 @@ void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
>   
>   		/* get rid of '/' in the compatible string to be able to find the FW */
>   		len = strlen(tmp) + 1;
> -		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
> -		strscpy(board_type, tmp, len);
> +		board_type = devm_kstrdup(dev, tmp, GFP_KERNEL);
> +		if (!board_type) {
> +			brcmf_err("out of memory allocating board_type\n");

Drop the message. mm will blurb out with a stack trace providing all the 
info you need.

> +			of_node_put(root);
> +			return;
> +		}
>   		for (i = 0; i < board_type[i]; i++) {
>   			if (board_type[i] == '/')
>   				board_type[i] = '-';
Andy Shevchenko Jan. 20, 2022, 10:52 a.m. UTC | #2
On Mon, Jan 17, 2022 at 4:31 PM Hector Martin <marcan@marcan.st> wrote:
>
> This was missing a NULL check, and we can collapse the strlen/alloc/copy
> into a devm_kstrdup().

Nice patch. After dropping the message,
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

>                 /* get rid of '/' in the compatible string to be able to find the FW */
>                 len = strlen(tmp) + 1;
> -               board_type = devm_kzalloc(dev, len, GFP_KERNEL);
> -               strscpy(board_type, tmp, len);
> +               board_type = devm_kstrdup(dev, tmp, GFP_KERNEL);
> +               if (!board_type) {
> +                       brcmf_err("out of memory allocating board_type\n");
> +                       of_node_put(root);
> +                       return;
> +               }

>                 for (i = 0; i < board_type[i]; i++) {
>                         if (board_type[i] == '/')
>                                 board_type[i] = '-';

Next step is to replace this with NIH strreplace()

And
  of_property_read_string_index(root, "compatible", 0, &tmp);
with
  of_property_read_string(root, "compatible", &tmp);

And might add an error check, but I believe if there is no compatible
property present, this can't be called.
diff mbox series

Patch

diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c
index 513c7e6421b2..5dc1e942e9e7 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/of.c
@@ -79,8 +79,12 @@  void brcmf_of_probe(struct device *dev, enum brcmf_bus_type bus_type,
 
 		/* get rid of '/' in the compatible string to be able to find the FW */
 		len = strlen(tmp) + 1;
-		board_type = devm_kzalloc(dev, len, GFP_KERNEL);
-		strscpy(board_type, tmp, len);
+		board_type = devm_kstrdup(dev, tmp, GFP_KERNEL);
+		if (!board_type) {
+			brcmf_err("out of memory allocating board_type\n");
+			of_node_put(root);
+			return;
+		}
 		for (i = 0; i < board_type[i]; i++) {
 			if (board_type[i] == '/')
 				board_type[i] = '-';