diff options
-rw-r--r-- | src/libutil/str_util.c | 26 | ||||
-rw-r--r-- | src/libutil/str_util.h | 13 |
2 files changed, 37 insertions, 2 deletions
diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c index 189d7a398..7ff2d9ae7 100644 --- a/src/libutil/str_util.c +++ b/src/libutil/str_util.c @@ -330,7 +330,7 @@ rspamd_gstring_icase_hash (gconstpointer key) #define HASZERO(x) ~(((((x) & ZEROMASK) + ZEROMASK) | (x)) | ZEROMASK) gsize -rspamd_strlcpy (gchar *dst, const gchar *src, gsize siz) +rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz) { gchar *d = dst; const gchar *s = src; @@ -371,6 +371,30 @@ rspamd_strlcpy (gchar *dst, const gchar *src, gsize siz) return (d - dst); } +size_t +rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz) +{ + const gchar *osrc = src; + gchar *d = dst; + gsize nleft = siz; + + if (nleft != 0) { + while (--nleft != 0) { + if ((*d++ = *src++) == '\0') { + break; + } + } + } + + if (nleft == 0) { + if (siz != 0) { + *d = '\0'; + } + } + + return (d - dst); +} + /* * Try to convert string of length to long */ diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h index 9fa914292..73637a62c 100644 --- a/src/libutil/str_util.h +++ b/src/libutil/str_util.h @@ -74,7 +74,18 @@ gboolean rspamd_gstring_icase_equal (gconstpointer v, gconstpointer v2); * @param siz length of destination buffer * @return bytes copied */ -gsize rspamd_strlcpy (gchar *dst, const gchar *src, gsize siz); +gsize rspamd_strlcpy_fast (gchar *dst, const gchar *src, gsize siz); +gsize rspamd_strlcpy_safe (gchar *dst, const gchar *src, gsize siz); + +#if defined(__has_feature) +# if __has_feature(address_sanitizer) +# define rspamd_strlcpy rspamd_strlcpy_safe +# else +# define rspamd_strlcpy rspamd_strlcpy_fast +# endif +#else +# define rspamd_strlcpy rspamd_strlcpy_fast +#endif /* * Try to convert string of length to long |