Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ASoC: 88pm860x: refactor deprecated strncpy

`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 care taken to ensure that the
destination buffer would be NUL-terminated. The destination buffer is
zero-initialized and each `pm860x->name[i]` has a size of
`MAX_NAME_LENGTH + 1`. This means that there is unlikely to be a bug
here.

However, in an attempt to eliminate the usage of the `strncpy` API as
well as disambiguate implementations, replacements such as: `strscpy`,
`strscpy_pad`, `strtomem` and `strtomem_pad` should be preferred.

We are able to eliminate the need for `len + 1` since `strscpy`
guarantees NUL-termination for its destination buffer as per its
implementation [3]:

| /* Hit buffer length without finding a NUL; force NUL-termination. */
| if (res)
| dest[res-1] = '\0';

[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>
Link: https://lore.kernel.org/r/20230727-sound-soc-codecs-v1-1-562fa2836bf4@google.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Justin Stitt and committed by
Mark Brown
a9a65b87 58f3c70c

+2 -2
+2 -2
sound/soc/codecs/88pm860x-codec.c
··· 143 143 struct pm860x_det det; 144 144 145 145 int irq[4]; 146 - unsigned char name[4][MAX_NAME_LEN+1]; 146 + unsigned char name[4][MAX_NAME_LEN]; 147 147 }; 148 148 149 149 /* -9450dB to 0dB in 150dB steps ( mute instead of -9450dB) */ ··· 1373 1373 return -EINVAL; 1374 1374 } 1375 1375 pm860x->irq[i] = res->start + chip->irq_base; 1376 - strncpy(pm860x->name[i], res->name, MAX_NAME_LEN); 1376 + strscpy(pm860x->name[i], res->name, MAX_NAME_LEN); 1377 1377 } 1378 1378 1379 1379 ret = devm_snd_soc_register_component(&pdev->dev,