From: Vsevolod Stakhov Date: Thu, 7 Feb 2019 15:47:16 +0000 (+0000) Subject: [Minor] Add rspamd_null_safe_copy routine X-Git-Tag: 1.9.0~199 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=11034d766c94f50f7c21ab6e9cffb07da736ea8f;p=rspamd.git [Minor] Add rspamd_null_safe_copy routine --- diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 7078415d3..25c88acee 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -391,6 +391,32 @@ rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz) return (d - dst); } +gsize +rspamd_null_safe_copy (const gchar *src, gsize srclen, + gchar *dest, gsize destlen) +{ + gsize copied = 0, si = 0, di = 0; + + if (destlen == 0) { + return 0; + } + + while (si < srclen && di + 1 < destlen) { + if (src[si] != '\0') { + dest[di++] = src[si++]; + copied ++; + } + else { + si ++; + } + } + + dest[di] = '\0'; + + return copied; +} + + size_t rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz) { diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index 72e0ab56f..46b74001b 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -89,6 +89,18 @@ gsize rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz); # define rspamd_strlcpy rspamd_strlcpy_fast #endif +/** + * Copies `srclen` characters from `src` to `dst` ignoring \0 + * @param src + * @param srclen + * @param dest + * @param destlen + * @return number of bytes copied + */ +gsize +rspamd_null_safe_copy (const gchar *src, gsize srclen, + gchar *dest, gsize destlen); + /* * Try to convert string of length to long */