commit 568773d: [Minor] Don't use zero terminated strings

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Dec 14 17:49:04 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-12-14 16:16:34 +0000
URL: https://github.com/rspamd/rspamd/commit/568773df1dcf5ed043ca0c760eaf53f1705ceb34

[Minor] Don't use zero terminated strings

---
 src/libutil/str_util.c | 23 ++++++++++++-----------
 src/libutil/str_util.h |  2 ++
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/src/libutil/str_util.c b/src/libutil/str_util.c
index 2d39ccf8a..50af10f28 100644
--- a/src/libutil/str_util.c
+++ b/src/libutil/str_util.c
@@ -1571,18 +1571,18 @@ rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len,
 }
 
 GString *
-rspamd_header_value_fold (const gchar *name,
-		const gchar *value,
-		guint fold_max,
-		enum rspamd_newlines_type how,
-		const gchar *fold_on_chars)
+rspamd_header_value_fold (const gchar *name, gsize name_len,
+						  const gchar *value,
+						  gsize value_len,
+						  guint fold_max,
+						  enum rspamd_newlines_type how,
+						  const gchar *fold_on_chars)
 {
 	GString *res;
 	const guint default_fold_max = 76;
 	guint cur_len;
-	const gchar *p, *c;
+	const gchar *p, *c, *end;
 	guint nspaces = 0;
-	const gchar *last;
 	gboolean first_token = TRUE;
 	enum {
 		fold_before = 0,
@@ -1603,14 +1603,15 @@ rspamd_header_value_fold (const gchar *name,
 		fold_max = default_fold_max;
 	}
 
-	res = g_string_sized_new (strlen (value));
+	res = g_string_sized_new (value_len);
 
 	c = value;
 	p = c;
+	end = value + value_len;
 	/* name:<WSP> */
-	cur_len = strlen (name) + 2;
+	cur_len = name_len + 2;
 
-	while (*p) {
+	while (p < end) {
 		switch (state) {
 
 		case read_token:
@@ -1697,7 +1698,7 @@ rspamd_header_value_fold (const gchar *name,
 					 * Check any spaces that are appended to the result
 					 * before folding
 					 */
-					last = &res->str[res->len - 1];
+					const gchar *last = &res->str[res->len - 1];
 
 					while (g_ascii_isspace (*last)) {
 						last --;
diff --git a/src/libutil/str_util.h b/src/libutil/str_util.h
index 47abf062e..199a384ca 100644
--- a/src/libutil/str_util.h
+++ b/src/libutil/str_util.h
@@ -366,7 +366,9 @@ gint rspamd_strings_levenshtein_distance (const gchar *s1, gsize s1len,
  * @return new GString with the folded value
  */
 GString *rspamd_header_value_fold (const gchar *name,
+								   gsize name_len,
 								   const gchar *value,
+								   gsize value_len,
 								   guint fold_max,
 								   enum rspamd_newlines_type how,
 								   const gchar *fold_on_chars);


More information about the Commits mailing list