aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libutil/str_util.c26
-rw-r--r--src/libutil/str_util.h12
2 files changed, 38 insertions, 0 deletions
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
*/