Date: Tue, 20 Jul 2021 23:46:27 GMT From: Vladimir Kondratyev <wulf@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 22310af02da3 - stable/13 - LinuxKPI: Implement strscpy Message-ID: <202107202346.16KNkRSZ054708@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by wulf: URL: https://cgit.FreeBSD.org/src/commit/?id=22310af02da3c40eae4a11b20019bcead508a0c9 commit 22310af02da3c40eae4a11b20019bcead508a0c9 Author: Vladimir Kondratyev <wulf@FreeBSD.org> AuthorDate: 2021-07-05 00:20:42 +0000 Commit: Vladimir Kondratyev <wulf@FreeBSD.org> CommitDate: 2021-07-20 22:43:52 +0000 LinuxKPI: Implement strscpy strscpy copies the src string, or as much of it as fits, into the dst buffer. The dst buffer is always NUL terminated, unless it's zero-sized. strscpy returns the number of characters copied (not including the trailing NUL) or -E2BIG if len is 0 or src was truncated. Currently drm-kmod replaces strscpy with strncpy that is not quite correct as strncpy does not NUL-terminate truncated strings and returns different values on exit. Reviewed by: hselasky, imp, manu Differential revision: https://reviews.freebsd.org/D31005 (cherry picked from commit 019391bf852771070cb739900f9e20ae6c41c746) --- sys/compat/linuxkpi/common/include/linux/string.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sys/compat/linuxkpi/common/include/linux/string.h b/sys/compat/linuxkpi/common/include/linux/string.h index 39201e203162..659a48d93596 100644 --- a/sys/compat/linuxkpi/common/include/linux/string.h +++ b/sys/compat/linuxkpi/common/include/linux/string.h @@ -167,4 +167,20 @@ str_has_prefix(const char *str, const char *prefix) return (strncmp(str, prefix, len) == 0 ? len : 0); } +static inline ssize_t +strscpy(char* dst, const char* src, size_t len) +{ + size_t i; + + if (len <= INT_MAX) { + for (i = 0; i < len; i++) + if ('\0' == (dst[i] = src[i])) + return ((ssize_t)i); + if (i != 0) + dst[--i] = '\0'; + } + + return (-E2BIG); +} + #endif /* _LINUX_STRING_H_ */
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202107202346.16KNkRSZ054708>