commit 11034d7: [Minor] Add rspamd_null_safe_copy routine

Vsevolod Stakhov vsevolod at highsecure.ru
Thu Feb 7 17:28:05 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-02-07 15:47:16 +0000
URL: https://github.com/rspamd/rspamd/commit/11034d766c94f50f7c21ab6e9cffb07da736ea8f

[Minor] Add rspamd_null_safe_copy routine

---
 src/libutil/str_util.c | 26 ++++++++++++++++++++++++++
 src/libutil/str_util.h | 12 ++++++++++++
 2 files changed, 38 insertions(+)

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
  */


More information about the Commits mailing list