All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ASoC: fsl_micfil: refactor deprecated strncpy
@ 2023-07-27 22:26 ` Justin Stitt
  0 siblings, 0 replies; 5+ messages in thread
From: Justin Stitt @ 2023-07-27 22:26 UTC (permalink / raw)
  To: Shengjiu Wang, Xiubo Li, Fabio Estevam, Nicolin Chen,
	Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: alsa-devel, linuxppc-dev, linux-kernel, Kees Cook, Justin Stitt

`strncpy` is deprecated for use on NUL-terminated destination strings [1].

A suitable replacement is `strscpy` [2] due to the fact that it
guarantees NUL-termination on its destination buffer argument which is
_not_ always the case for `strncpy`!

In this case, though, there was great care taken to ensure that the
destination buffer would be NUL-terminated through the use of `len - 1`
ensuring that the previously zero-initialized buffer would not overwrite
the last NUL byte. This means that there's no bug here.

However, `strscpy` will add a mandatory NUL byte to the destination
buffer as promised by the following `strscpy` implementation [3]:
|       /* Hit buffer length without finding a NUL; force NUL-termination. */
|       if (res)
|               dest[res-1] = '\0';

This means we can lose the `- 1` which clears up whats happening here.
All the while, we get one step closer to eliminating the ambiguous
`strncpy` api in favor of its less ambiguous replacement like `strscpy`,
`strscpy_pad`, `strtomem` and `strtomem_pad` amongst others.

[1]: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
[2]: manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
[3]: https://elixir.bootlin.com/linux/v6.3/source/lib/string.c#L183

Link: https://github.com/KSPP/linux/issues/90
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
 sound/soc/fsl/fsl_micfil.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/soc/fsl/fsl_micfil.c b/sound/soc/fsl/fsl_micfil.c
index 3f08082a55be..fe28b27e50d0 100644
--- a/sound/soc/fsl/fsl_micfil.c
+++ b/sound/soc/fsl/fsl_micfil.c
@@ -1044,7 +1044,7 @@ static int fsl_micfil_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	micfil->pdev = pdev;
-	strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
+	strscpy(micfil->name, np->name, sizeof(micfil->name));
 
 	micfil->soc = of_device_get_match_data(&pdev->dev);
 

---
base-commit: 57012c57536f8814dec92e74197ee96c3498d24e
change-id: 20230727-sound-soc-fsl-4fc5569d771e

Best regards,
--
Justin Stitt <justinstitt@google.com>


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH] ASoC: fsl_micfil: refactor deprecated strncpy
@ 2023-07-27 22:26 ` Justin Stitt
  0 siblings, 0 replies; 5+ messages in thread
From: Justin Stitt @ 2023-07-27 22:26 UTC (permalink / raw)
  To: Shengjiu Wang, Xiubo Li, Fabio Estevam, Nicolin Chen,
	Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai
  Cc: alsa-devel, Justin Stitt, linuxppc-dev, linux-kernel, Kees Cook

`strncpy` is deprecated for use on NUL-terminated destination strings [1].

A suitable replacement is `strscpy` [2] due to the fact that it
guarantees NUL-termination on its destination buffer argument which is
_not_ always the case for `strncpy`!

In this case, though, there was great care taken to ensure that the
destination buffer would be NUL-terminated through the use of `len - 1`
ensuring that the previously zero-initialized buffer would not overwrite
the last NUL byte. This means that there's no bug here.

However, `strscpy` will add a mandatory NUL byte to the destination
buffer as promised by the following `strscpy` implementation [3]:
|       /* Hit buffer length without finding a NUL; force NUL-termination. */
|       if (res)
|               dest[res-1] = '\0';

This means we can lose the `- 1` which clears up whats happening here.
All the while, we get one step closer to eliminating the ambiguous
`strncpy` api in favor of its less ambiguous replacement like `strscpy`,
`strscpy_pad`, `strtomem` and `strtomem_pad` amongst others.

[1]: www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
[2]: manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
[3]: https://elixir.bootlin.com/linux/v6.3/source/lib/string.c#L183

Link: https://github.com/KSPP/linux/issues/90
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
 sound/soc/fsl/fsl_micfil.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sound/soc/fsl/fsl_micfil.c b/sound/soc/fsl/fsl_micfil.c
index 3f08082a55be..fe28b27e50d0 100644
--- a/sound/soc/fsl/fsl_micfil.c
+++ b/sound/soc/fsl/fsl_micfil.c
@@ -1044,7 +1044,7 @@ static int fsl_micfil_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	micfil->pdev = pdev;
-	strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
+	strscpy(micfil->name, np->name, sizeof(micfil->name));
 
 	micfil->soc = of_device_get_match_data(&pdev->dev);
 

---
base-commit: 57012c57536f8814dec92e74197ee96c3498d24e
change-id: 20230727-sound-soc-fsl-4fc5569d771e

Best regards,
--
Justin Stitt <justinstitt@google.com>


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ASoC: fsl_micfil: refactor deprecated strncpy
  2023-07-27 22:26 ` Justin Stitt
@ 2023-07-28  6:09   ` Shengjiu Wang
  -1 siblings, 0 replies; 5+ messages in thread
From: Shengjiu Wang @ 2023-07-28  6:09 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Xiubo Li, Fabio Estevam, Nicolin Chen, Liam Girdwood, Mark Brown,
	Jaroslav Kysela, Takashi Iwai, alsa-devel, linuxppc-dev,
	linux-kernel, Kees Cook

On Fri, Jul 28, 2023 at 6:26 AM Justin Stitt <justinstitt@google.com> wrote:

> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on its destination buffer argument which is
> _not_ always the case for `strncpy`!
>
> In this case, though, there was great care taken to ensure that the
> destination buffer would be NUL-terminated through the use of `len - 1`
> ensuring that the previously zero-initialized buffer would not overwrite
> the last NUL byte. This means that there's no bug here.
>
> However, `strscpy` will add a mandatory NUL byte to the destination
> buffer as promised by the following `strscpy` implementation [3]:
> |       /* Hit buffer length without finding a NUL; force NUL-termination.
> */
> |       if (res)
> |               dest[res-1] = '\0';
>
> This means we can lose the `- 1` which clears up whats happening here.
> All the while, we get one step closer to eliminating the ambiguous
> `strncpy` api in favor of its less ambiguous replacement like `strscpy`,
> `strscpy_pad`, `strtomem` and `strtomem_pad` amongst others.
>
> [1]:
> www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> [2]: manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
> [3]: https://elixir.bootlin.com/linux/v6.3/source/lib/string.c#L183
>
> Link: https://github.com/KSPP/linux/issues/90
> Signed-off-by: Justin Stitt <justinstitt@google.com>
>

Acked-by: Shengjiu Wang <shengjiu.wang@gmail.com>

Best regards
wang shengjiu

> ---
>  sound/soc/fsl/fsl_micfil.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/sound/soc/fsl/fsl_micfil.c b/sound/soc/fsl/fsl_micfil.c
> index 3f08082a55be..fe28b27e50d0 100644
> --- a/sound/soc/fsl/fsl_micfil.c
> +++ b/sound/soc/fsl/fsl_micfil.c
> @@ -1044,7 +1044,7 @@ static int fsl_micfil_probe(struct platform_device
> *pdev)
>                 return -ENOMEM;
>
>         micfil->pdev = pdev;
> -       strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
> +       strscpy(micfil->name, np->name, sizeof(micfil->name));
>
>         micfil->soc = of_device_get_match_data(&pdev->dev);
>
>
> ---
> base-commit: 57012c57536f8814dec92e74197ee96c3498d24e
> change-id: 20230727-sound-soc-fsl-4fc5569d771e
>
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ASoC: fsl_micfil: refactor deprecated strncpy
@ 2023-07-28  6:09   ` Shengjiu Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Shengjiu Wang @ 2023-07-28  6:09 UTC (permalink / raw)
  To: Justin Stitt
  Cc: alsa-devel, Kees Cook, Xiubo Li, linuxppc-dev, Takashi Iwai,
	Liam Girdwood, Jaroslav Kysela, Nicolin Chen, Mark Brown,
	Fabio Estevam, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2479 bytes --]

On Fri, Jul 28, 2023 at 6:26 AM Justin Stitt <justinstitt@google.com> wrote:

> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
>
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on its destination buffer argument which is
> _not_ always the case for `strncpy`!
>
> In this case, though, there was great care taken to ensure that the
> destination buffer would be NUL-terminated through the use of `len - 1`
> ensuring that the previously zero-initialized buffer would not overwrite
> the last NUL byte. This means that there's no bug here.
>
> However, `strscpy` will add a mandatory NUL byte to the destination
> buffer as promised by the following `strscpy` implementation [3]:
> |       /* Hit buffer length without finding a NUL; force NUL-termination.
> */
> |       if (res)
> |               dest[res-1] = '\0';
>
> This means we can lose the `- 1` which clears up whats happening here.
> All the while, we get one step closer to eliminating the ambiguous
> `strncpy` api in favor of its less ambiguous replacement like `strscpy`,
> `strscpy_pad`, `strtomem` and `strtomem_pad` amongst others.
>
> [1]:
> www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings
> [2]: manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html
> [3]: https://elixir.bootlin.com/linux/v6.3/source/lib/string.c#L183
>
> Link: https://github.com/KSPP/linux/issues/90
> Signed-off-by: Justin Stitt <justinstitt@google.com>
>

Acked-by: Shengjiu Wang <shengjiu.wang@gmail.com>

Best regards
wang shengjiu

> ---
>  sound/soc/fsl/fsl_micfil.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/sound/soc/fsl/fsl_micfil.c b/sound/soc/fsl/fsl_micfil.c
> index 3f08082a55be..fe28b27e50d0 100644
> --- a/sound/soc/fsl/fsl_micfil.c
> +++ b/sound/soc/fsl/fsl_micfil.c
> @@ -1044,7 +1044,7 @@ static int fsl_micfil_probe(struct platform_device
> *pdev)
>                 return -ENOMEM;
>
>         micfil->pdev = pdev;
> -       strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);
> +       strscpy(micfil->name, np->name, sizeof(micfil->name));
>
>         micfil->soc = of_device_get_match_data(&pdev->dev);
>
>
> ---
> base-commit: 57012c57536f8814dec92e74197ee96c3498d24e
> change-id: 20230727-sound-soc-fsl-4fc5569d771e
>
> Best regards,
> --
> Justin Stitt <justinstitt@google.com>
>
>

[-- Attachment #2: Type: text/html, Size: 3804 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ASoC: fsl_micfil: refactor deprecated strncpy
  2023-07-27 22:26 ` Justin Stitt
  (?)
  (?)
@ 2023-07-28 16:35 ` Mark Brown
  -1 siblings, 0 replies; 5+ messages in thread
From: Mark Brown @ 2023-07-28 16:35 UTC (permalink / raw)
  To: Shengjiu Wang, Xiubo Li, Fabio Estevam, Nicolin Chen,
	Liam Girdwood, Jaroslav Kysela, Takashi Iwai, Justin Stitt
  Cc: alsa-devel, linuxppc-dev, linux-kernel, Kees Cook

On Thu, 27 Jul 2023 22:26:41 +0000, Justin Stitt wrote:
> `strncpy` is deprecated for use on NUL-terminated destination strings [1].
> 
> A suitable replacement is `strscpy` [2] due to the fact that it
> guarantees NUL-termination on its destination buffer argument which is
> _not_ always the case for `strncpy`!
> 
> In this case, though, there was great care taken to ensure that the
> destination buffer would be NUL-terminated through the use of `len - 1`
> ensuring that the previously zero-initialized buffer would not overwrite
> the last NUL byte. This means that there's no bug here.
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/1] ASoC: fsl_micfil: refactor deprecated strncpy
      commit: 7eb10bfbbae6025cb7b4bba3db0c1281eac05862

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-07-28 16:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-27 22:26 [PATCH] ASoC: fsl_micfil: refactor deprecated strncpy Justin Stitt
2023-07-27 22:26 ` Justin Stitt
2023-07-28  6:09 ` Shengjiu Wang
2023-07-28  6:09   ` Shengjiu Wang
2023-07-28 16:35 ` Mark Brown

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.