changeset 8167:99704b6b8dde HEAD

message address parser: Handle errors better, fix crash in previous change.
author Timo Sirainen <tss@iki.fi>
date Sun, 07 Sep 2008 19:25:29 +0300
parents a1f6c93afbc2
children 55871f3b4481
files src/lib-mail/message-address.c
diffstat 1 files changed, 23 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-mail/message-address.c	Sun Sep 07 17:59:22 2008 +0300
+++ b/src/lib-mail/message-address.c	Sun Sep 07 19:25:29 2008 +0300
@@ -174,6 +174,15 @@
 	return ret;
 }
 
+static void add_fixed_address(struct message_address_parser_context *ctx)
+{
+	if (ctx->addr.mailbox == NULL)
+		ctx->addr.mailbox = !ctx->fill_missing ? "" : "MISSING_MAILBOX";
+	if (ctx->addr.domain == NULL)
+		ctx->addr.domain = !ctx->fill_missing ? "" : "MISSING_DOMAIN";
+	add_address(ctx);
+}
+
 static int parse_mailbox(struct message_address_parser_context *ctx)
 {
 	const unsigned char *start;
@@ -191,16 +200,7 @@
 			return -1;
 	}
 
-	if (ctx->addr.mailbox == NULL) {
-		ctx->addr.mailbox = !ctx->fill_missing ? "" :
-			p_strdup(ctx->pool, "MISSING_MAILBOX");
-	}
-	if (ctx->addr.domain == NULL) {
-		ctx->addr.domain = !ctx->fill_missing ? "" :
-			p_strdup(ctx->pool, "MISSING_DOMAIN");
-	}
-	add_address(ctx);
-
+	add_fixed_address(ctx);
 	return ret;
 }
 
@@ -289,6 +289,7 @@
 			   unsigned int max_addresses, bool fill_missing)
 {
 	struct message_address_parser_context ctx;
+	int ret;
 
 	memset(&ctx, 0, sizeof(ctx));
 
@@ -297,10 +298,19 @@
 	ctx.str = t_str_new(128);
 	ctx.fill_missing = fill_missing;
 
-	rfc822_skip_lwsp(&ctx.parser);
-
-	if (parse_address_list(&ctx, max_addresses) < 0)
+	ret = rfc822_skip_lwsp(&ctx.parser);
+	if (ret == 0) {
+		/* no addresses */
+		return NULL;
+	}
+	if (ret < 0 || parse_address_list(&ctx, max_addresses) < 0) {
+		if (ctx.first_addr == NULL) {
+			/* we had some input - we'll have to return something
+			   so that we can set invalid_syntax */
+			add_fixed_address(&ctx);
+		}
 		ctx.first_addr->invalid_syntax = TRUE;
+	}
 	return ctx.first_addr;
 }