[PATCH 36/74] backports: support strscpy_pad in older kernels
Johannes Berg <[email protected]> Fri, 24 May 2024 19:07:48 +0200
| Newsgroups | org.kernel.vger.backports |
|---|---|
| Message-ID | <20240524190907.1dee9821fe79.I57f107eb082c9a5d4ba9377bbf6c777013d7e24a@changeid> |
From: Miri Korenblit <[email protected]> This was added in kernel v5.2. Add support for it in older kernels. Signed-off-by: Miri Korenblit <[email protected]> Signed-off-by: Johannes Berg <[email protected]> --- backport/backport-include/linux/string.h | 5 +++++ backport/compat/backport-5.2.c | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/backport/backport-include/linux/string.h b/backport/backport-include/linux/string.h index ce4c99e34e0d..f6e6cdcf08b8 100644 --- a/backport/backport-include/linux/string.h +++ b/backport/backport-include/linux/string.h @@ -23,4 +23,9 @@ }) #endif +#if LINUX_VERSION_IS_LESS(5,2,0) +#define strscpy_pad LINUX_BACKPORT(strscpy_pad) +ssize_t strscpy_pad(char *dest, const char *src, size_t count); +#endif + #endif /* __BACKPORT_LINUX_STRING_H */ diff --git a/backport/compat/backport-5.2.c b/backport/compat/backport-5.2.c index 5624d5fa08e5..581bb9cfe7fd 100644 --- a/backport/compat/backport-5.2.c +++ b/backport/compat/backport-5.2.c @@ -479,3 +479,17 @@ int __nla_parse(struct nlattr **tb, int maxtype, extack, tb); } EXPORT_SYMBOL(__nla_parse); + +ssize_t strscpy_pad(char *dest, const char *src, size_t count) +{ + ssize_t written; + + written = strscpy(dest, src, count); + if (written < 0 || written == count - 1) + return written; + + memset(dest + written + 1, 0, count - written - 1); + + return written; +} +EXPORT_SYMBOL(strscpy_pad); -- 2.45.1