mirror of
https://github.com/opnsense/src.git
synced 2026-06-09 00:32:25 -04:00
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 019391bf85)
This commit is contained in:
parent
80b8638e12
commit
22310af02d
1 changed files with 16 additions and 0 deletions
|
|
@ -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_ */
|
||||
|
|
|
|||
Loading…
Reference in a new issue