<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jul 28, 2023 at 6:26 AM Justin Stitt <<a href="mailto:justinstitt@google.com">justinstitt@google.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">`strncpy` is deprecated for use on NUL-terminated destination strings [1].<br>
<br>
A suitable replacement is `strscpy` [2] due to the fact that it<br>
guarantees NUL-termination on its destination buffer argument which is<br>
_not_ always the case for `strncpy`!<br>
<br>
In this case, though, there was great care taken to ensure that the<br>
destination buffer would be NUL-terminated through the use of `len - 1`<br>
ensuring that the previously zero-initialized buffer would not overwrite<br>
the last NUL byte. This means that there's no bug here.<br>
<br>
However, `strscpy` will add a mandatory NUL byte to the destination<br>
buffer as promised by the following `strscpy` implementation [3]:<br>
|       /* Hit buffer length without finding a NUL; force NUL-termination. */<br>
|       if (res)<br>
|               dest[res-1] = '\0';<br>
<br>
This means we can lose the `- 1` which clears up whats happening here.<br>
All the while, we get one step closer to eliminating the ambiguous<br>
`strncpy` api in favor of its less ambiguous replacement like `strscpy`,<br>
`strscpy_pad`, `strtomem` and `strtomem_pad` amongst others.<br>
<br>
[1]: <a href="http://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings" rel="noreferrer" target="_blank">www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings</a><br>
[2]: <a href="http://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html" rel="noreferrer" target="_blank">manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html</a><br>
[3]: <a href="https://elixir.bootlin.com/linux/v6.3/source/lib/string.c#L183" rel="noreferrer" target="_blank">https://elixir.bootlin.com/linux/v6.3/source/lib/string.c#L183</a><br>
<br>
Link: <a href="https://github.com/KSPP/linux/issues/90" rel="noreferrer" target="_blank">https://github.com/KSPP/linux/issues/90</a><br>
Signed-off-by: Justin Stitt <<a href="mailto:justinstitt@google.com" target="_blank">justinstitt@google.com</a>><br></blockquote><div><br></div><div>Acked-by: Shengjiu Wang <<a href="mailto:shengjiu.wang@gmail.com">shengjiu.wang@gmail.com</a>></div><div><br></div><div>Best regards</div><div>wang shengjiu </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
---<br>
 sound/soc/fsl/fsl_micfil.c | 2 +-<br>
 1 file changed, 1 insertion(+), 1 deletion(-)<br>
<br>
diff --git a/sound/soc/fsl/fsl_micfil.c b/sound/soc/fsl/fsl_micfil.c<br>
index 3f08082a55be..fe28b27e50d0 100644<br>
--- a/sound/soc/fsl/fsl_micfil.c<br>
+++ b/sound/soc/fsl/fsl_micfil.c<br>
@@ -1044,7 +1044,7 @@ static int fsl_micfil_probe(struct platform_device *pdev)<br>
                return -ENOMEM;<br>
<br>
        micfil->pdev = pdev;<br>
-       strncpy(micfil->name, np->name, sizeof(micfil->name) - 1);<br>
+       strscpy(micfil->name, np->name, sizeof(micfil->name));<br>
<br>
        micfil->soc = of_device_get_match_data(&pdev->dev);<br>
<br>
<br>
---<br>
base-commit: 57012c57536f8814dec92e74197ee96c3498d24e<br>
change-id: 20230727-sound-soc-fsl-4fc5569d771e<br>
<br>
Best regards,<br>
--<br>
Justin Stitt <<a href="mailto:justinstitt@google.com" target="_blank">justinstitt@google.com</a>><br>
<br>
</blockquote></div></div>