# HG changeset patch # User Timo Sirainen # Date 1220804729 -10800 # Node ID 99704b6b8dde7f19299c1cab026f21b194745ec4 # Parent a1f6c93afbc2d5091c09345400f55994a2d243ac message address parser: Handle errors better, fix crash in previous change. diff -r a1f6c93afbc2 -r 99704b6b8dde src/lib-mail/message-address.c --- 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; }