diff options
author | cztchoice <cztchoice@gmail.com> | 2015-07-18 17:27:05 +0800 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2017-01-23 15:49:36 +0100 |
commit | 6e75bb5cbbf172bf586b3326ac43bad219ae26a3 (patch) | |
tree | 8469ad21d44fa6afa16c95a0a5504dd4bfbf66d0 /src/nvim/memory.c | |
parent | d4b931deacf61528e902623d38d0f4d314bc1839 (diff) | |
download | rneovim-6e75bb5cbbf172bf586b3326ac43bad219ae26a3.tar.gz rneovim-6e75bb5cbbf172bf586b3326ac43bad219ae26a3.tar.bz2 rneovim-6e75bb5cbbf172bf586b3326ac43bad219ae26a3.zip |
refactor: strlcat instead of str{n}cat.
Add xstrlcat function.
Closes #3042
References #988
References #1069
coverity: 71530, 71531, 71532
Diffstat (limited to 'src/nvim/memory.c')
-rw-r--r-- | src/nvim/memory.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/nvim/memory.c b/src/nvim/memory.c index 92ead873ae..6408ac1664 100644 --- a/src/nvim/memory.c +++ b/src/nvim/memory.c @@ -389,6 +389,33 @@ size_t xstrlcpy(char *restrict dst, const char *restrict src, size_t size) return ret; } +/// xstrlcat - Appends string src to the end of dst. +/// +/// Compatible with *BSD strlcat: Appends at most (dstsize - strlen(dst) - 1) +/// characters. dst will be NUL-terminated. +/// +/// Note: Replaces `vim_strcat`. +/// +/// @param dst Where to copy the string to +/// @param src Where to copy the string from +/// @param dstsize Size of destination buffer, must be greater than 0 +/// @return strlen(src) + MIN(dstsize, strlen(initial dst)). +/// If retval >= dstsize, truncation occurs. +size_t xstrlcat(char *restrict dst, const char *restrict src, size_t dstsize) + FUNC_ATTR_NONNULL_ALL +{ + assert(dstsize > 0); + size_t srclen = strlen(src); + size_t dstlen = strlen(dst); + size_t ret = srclen + dstlen; // Total string length (excludes NUL) + if (srclen) { + size_t len = (ret >= dstsize) ? dstsize - 1 : ret; + memcpy(dst + dstlen, src, len - dstlen); + dst[len] = '\0'; + } + return ret; // Does not include NUL. +} + /// strdup() wrapper /// /// @see {xmalloc} |