mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
memory: add xstrlcpy
Less "blow a hole in your foot" than strncpy. As also indicated by coverity. Implementation inspired by the linux kernel (very similar to OSX's Libc implementation as well).
This commit is contained in:
parent
3a68a4861a
commit
a50a34f472
@ -183,6 +183,19 @@ char *xstpncpy(char *restrict dst, const char *restrict src, size_t maxlen)
|
||||
}
|
||||
}
|
||||
|
||||
size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size)
|
||||
{
|
||||
size_t ret = strlen(src);
|
||||
|
||||
if (size) {
|
||||
size_t len = (ret >= size) ? size - 1 : ret;
|
||||
memcpy(dst, src, len);
|
||||
dst[len] = '\0';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *xstrdup(const char *str)
|
||||
{
|
||||
char *ret = strdup(str);
|
||||
|
@ -125,6 +125,19 @@ char *xstpcpy(char *restrict dst, const char *restrict src);
|
||||
/// @param maxlen
|
||||
char *xstpncpy(char *restrict dst, const char *restrict src, size_t maxlen);
|
||||
|
||||
/// xstrlcpy - Copy a %NUL terminated string into a sized buffer
|
||||
///
|
||||
/// Compatible with *BSD strlcpy: the result is always a valid
|
||||
/// NUL-terminated string that fits in the buffer (unless,
|
||||
/// of course, the buffer size is zero). It does not pad
|
||||
/// out the result like strncpy() does.
|
||||
///
|
||||
/// @param dst Where to copy the string to
|
||||
/// @param src Where to copy the string from
|
||||
/// @param size Size of destination buffer
|
||||
/// @return Length of the source string (i.e.: strlen(src))
|
||||
size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size);
|
||||
|
||||
/// Duplicates a chunk of memory using xmalloc
|
||||
///
|
||||
/// @see {xmalloc}
|
||||
|
Loading…
Reference in New Issue
Block a user