Skip to content

Commit

Permalink
ASoC: 88pm860x: refactor deprecated strncpy
Browse files Browse the repository at this point in the history
`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: KSPP#90
Signed-off-by: Justin Stitt <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Mark Brown <[email protected]>
  • Loading branch information
JustinStitt authored and broonie committed Jul 28, 2023
1 parent 58f3c70 commit a9a65b8
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions sound/soc/codecs/88pm860x-codec.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ struct pm860x_priv {
struct pm860x_det det;

int irq[4];
unsigned char name[4][MAX_NAME_LEN+1];
unsigned char name[4][MAX_NAME_LEN];
};

/* -9450dB to 0dB in 150dB steps ( mute instead of -9450dB) */
Expand Down Expand Up @@ -1373,7 +1373,7 @@ static int pm860x_codec_probe(struct platform_device *pdev)
return -EINVAL;
}
pm860x->irq[i] = res->start + chip->irq_base;
strncpy(pm860x->name[i], res->name, MAX_NAME_LEN);
strscpy(pm860x->name[i], res->name, MAX_NAME_LEN);
}

ret = devm_snd_soc_register_component(&pdev->dev,
Expand Down

0 comments on commit a9a65b8

Please sign in to comment.