dovecot-sieve-1.1: If we detect header is corrupted while unfold...

dovecot at dovecot.org dovecot at dovecot.org
Thu Feb 19 20:14:33 EET 2009


details:   http://hg.dovecot.org/dovecot-sieve-1.1/rev/45d2da01f907
changeset: 64:45d2da01f907
user:      Timo Sirainen <tss at iki.fi>
date:      Thu Feb 19 13:14:29 2009 -0500
description:
If we detect header is corrupted while unfolding, mark the cache file corrupted and retry.

diffstat:

1 file changed, 32 insertions(+), 15 deletions(-)
src/sieve-cmu.c |   47 ++++++++++++++++++++++++++++++++---------------

diffs (95 lines):

diff -r 49537bad6797 -r 45d2da01f907 src/sieve-cmu.c
--- a/src/sieve-cmu.c	Sun Dec 21 18:59:57 2008 +0200
+++ b/src/sieve-cmu.c	Thu Feb 19 13:14:29 2009 -0500
@@ -66,8 +66,9 @@ dovecot_sieve_compile(script_data_t *sda
 dovecot_sieve_compile(script_data_t *sdata, const char *script_path,
 		      const char *compiled_path);
 
-static const char *unfold_header(const char *str)
-{
+static int unfold_header(const char **_str)
+{
+	const char *str = *_str;
 	char *new_str;
 	unsigned int i, j;
 
@@ -76,7 +77,7 @@ static const char *unfold_header(const c
 			break;
 	}
 	if (str[i] == '\0')
-		return str;
+		return 0;
 
 	/* @UNSAFE */
 	new_str = t_malloc(i + strlen(str+i) + 1);
@@ -87,18 +88,22 @@ static const char *unfold_header(const c
 			i++;
 			if (str[i] == '\0')
 				break;
-			i_assert(str[i] == ' ' || str[i] == '\t');
+			if (str[i] != ' ' && str[i] != '\t') {
+				/* corrupted */
+				return -1;
+			}
 		} else {
 			new_str[j++] = str[i];
 		}
 	}
 	new_str[j] = '\0';
-	return new_str;
-}
-
-static const char *const *
-unfold_multiline_headers(const char *const *headers)
-{
+	*_str = new_str;
+	return 0;
+}
+
+static int unfold_multiline_headers(const char *const **_headers)
+{
+	const char *const *headers = *_headers;
 	const char **new_headers;
 	unsigned int i;
 
@@ -109,15 +114,19 @@ unfold_multiline_headers(const char *con
 	}
 	if (headers[i] == NULL) {
 		/* no multilines */
-		return headers;
+		return 0;
 	}
 
 	/* @UNSAFE */
 	for (; headers[i] != NULL; i++) ;
 	new_headers = t_new(const char *, i + 1);
-	for (i = 0; headers[i] != NULL; i++)
-		new_headers[i] = unfold_header(headers[i]);
-	return new_headers;
+	for (i = 0; headers[i] != NULL; i++) {
+		new_headers[i] = headers[i];
+		if (unfold_header(&new_headers[i]) < 0)
+			return -1;
+	}
+	*_headers = new_headers;
+	return 0;
 }
 
 /* gets the header "head" from msg. */
@@ -129,7 +138,15 @@ static int getheader(void *v, const char
     if (phead==NULL) return SIEVE_FAIL;
     if (mail_get_headers_utf8(m->mail, phead, &headers) < 0)
 	    return SIEVE_FAIL;
-    headers = unfold_multiline_headers(headers);
+    if (unfold_multiline_headers(&headers) < 0) {
+	    mail_set_cache_corrupted(m->mail, 0);
+	    if (mail_get_headers_utf8(m->mail, phead, &headers) < 0)
+		    return SIEVE_FAIL;
+	    if (unfold_multiline_headers(&headers) < 0) {
+		    i_error("Couldn't fix broken header unfolding");
+		    return SIEVE_FAIL;
+	    }
+    }
     *body = (const char **)headers;
 
     if (**body) {


More information about the dovecot-cvs mailing list