]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Don't assert-crash on mail search if decomposed titlecase of search key is empty.
authorTimo Sirainen <tss@iki.fi>
Tue, 10 Jan 2012 21:28:03 +0000 (23:28 +0200)
committerTimo Sirainen <tss@iki.fi>
Tue, 10 Jan 2012 21:28:03 +0000 (23:28 +0200)
src/lib-mail/message-search.c
src/lib-mail/message-search.h
src/lib-storage/index/index-search.c

index f024c322bae006d8bf9662f3eb041c95243b5102..2cd44b84689dddb6cca6dda8de39f1c4952f34ab 100644 (file)
@@ -5,7 +5,6 @@
 #include "istream.h"
 #include "str.h"
 #include "str-find.h"
-#include "unichar.h"
 #include "rfc822-parser.h"
 #include "message-decoder.h"
 #include "message-parser.h"
@@ -24,20 +23,18 @@ struct message_search_context *
 message_search_init(const char *key_utf8,
                    enum message_search_flags flags)
 {
+       enum message_decoder_flags decoder_flags = 0;
        struct message_search_context *ctx;
 
-       ctx = i_new(struct message_search_context, 1);
-       ctx->flags = flags;
-       ctx->decoder = message_decoder_init(MESSAGE_DECODER_FLAG_DTCASE);
+       i_assert(*key_utf8 != '\0');
 
-       T_BEGIN {
-               string_t *dtc = t_str_new(128);
+       if ((flags & MESSAGE_SEARCH_FLAG_DTCASE) != 0)
+               decoder_flags |= MESSAGE_DECODER_FLAG_DTCASE;
 
-               if (uni_utf8_to_decomposed_titlecase(key_utf8, strlen(key_utf8),
-                                                    dtc) < 0)
-                       i_panic("message_search_init(): key not utf8");
-               ctx->str_find_ctx = str_find_init(default_pool, str_c(dtc));
-       } T_END;
+       ctx = i_new(struct message_search_context, 1);
+       ctx->flags = flags;
+       ctx->decoder = message_decoder_init(decoder_flags);
+       ctx->str_find_ctx = str_find_init(default_pool, key_utf8);
        return ctx;
 }
 
index 1cdba694bedae796b0592e19ce95a4935ec25ec0..207756dd0c9fa27f2ff03bd7bdda07bfe78d599c 100644 (file)
@@ -7,7 +7,10 @@ struct message_search_context;
 
 enum message_search_flags {
        /* Skip the main header and all the MIME headers. */
-       MESSAGE_SEARCH_FLAG_SKIP_HEADERS        = 0x01
+       MESSAGE_SEARCH_FLAG_SKIP_HEADERS        = 0x01,
+       /* Search with decomposed titlecase (instead of exact case matching).
+          The search key must be given with dtcase also. */
+       MESSAGE_SEARCH_FLAG_DTCASE              = 0x02
 };
 
 /* The key must be given in UTF-8 charset */
index d66683277a305aca3ca1e97dc3dfabf32562a4d2..2fee36b2a6529d4049fa5236cc96891606708689 100644 (file)
@@ -7,6 +7,7 @@
 #include "utc-offset.h"
 #include "str.h"
 #include "time-util.h"
+#include "unichar.h"
 #include "imap-match.h"
 #include "message-address.h"
 #include "message-date.h"
@@ -373,13 +374,24 @@ static int search_sent(enum mail_search_arg_type type, time_t search_time,
 static struct message_search_context *
 msg_search_arg_context(struct mail_search_arg *arg)
 {
-       enum message_search_flags flags;
-
-       if (arg->context == NULL) {
-               flags = arg->type == SEARCH_BODY ?
-                       MESSAGE_SEARCH_FLAG_SKIP_HEADERS : 0;
-               arg->context = message_search_init(arg->value.str, flags);
-       }
+       enum message_search_flags flags = MESSAGE_SEARCH_FLAG_DTCASE;
+
+       if (arg->context == NULL) T_BEGIN {
+               string_t *dtc = t_str_new(128);
+
+               if (uni_utf8_to_decomposed_titlecase(arg->value.str,
+                                                    strlen(arg->value.str),
+                                                    dtc) < 0)
+                       i_panic("search key not utf8: %s", arg->value.str);
+
+               if (arg->type == SEARCH_BODY)
+                       flags |= MESSAGE_SEARCH_FLAG_SKIP_HEADERS;
+               /* we don't get here if arg is "", but dtc can be "" if it
+                  only contains characters that we need to ignore. handle
+                  those searches by returning them as non-matched. */
+               if (str_len(dtc) > 0)
+                       arg->context = message_search_init(str_c(dtc), flags);
+       } T_END;
        return arg->context;
 }