changeset 19651:b77fff55d62c

pop3c: Fixed assert-crash when prefetching a mail failed.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Wed, 27 Jan 2016 02:04:00 +0200
parents 44b26f969840
children ebc657852018
files src/lib-storage/index/pop3c/pop3c-mail.c src/lib-storage/index/pop3c/pop3c-storage.h
diffstat 2 files changed, 18 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/pop3c/pop3c-mail.c	Wed Jan 27 01:12:21 2016 +0200
+++ b/src/lib-storage/index/pop3c/pop3c-mail.c	Wed Jan 27 02:04:00 2016 +0200
@@ -33,6 +33,8 @@
 	/* wait for any prefetch to finish before closing the mail */
 	while (pmail->prefetching)
 		pop3c_client_wait_one(mbox->client);
+	if (pmail->prefetch_stream != NULL)
+		i_stream_unref(&pmail->prefetch_stream);
 	index_mail_close(_mail);
 }
 
@@ -131,16 +133,20 @@
 				     const char *reply, void *context)
 {
 	struct pop3c_mail *pmail = context;
+	struct mail *_mail = &pmail->imail.mail.mail;
+	const char *cmd;
 
 	switch (state) {
 	case POP3C_COMMAND_STATE_OK:
 		break;
 	case POP3C_COMMAND_STATE_ERR:
 	case POP3C_COMMAND_STATE_DISCONNECTED:
-		i_stream_unref(&pmail->imail.data.stream);
-		pmail->imail.data.stream =
-			i_stream_create_error_str(EIO, "%s failed: %s",
-				pmail->prefetching_body ? "RETR" : "TOP", reply);
+		cmd = pmail->prefetching_body ? "RETR" : "TOP";
+		i_stream_unref(&pmail->prefetch_stream);
+		pmail->prefetch_stream =
+			i_stream_create_error_str(EIO, "%s %u failed: %s",
+				cmd, _mail->seq, reply);
+		i_stream_set_name(pmail->prefetch_stream, cmd);
 		break;
 	}
 	pmail->prefetching = FALSE;
@@ -164,10 +170,10 @@
 			cmd = t_strdup_printf("TOP %u 0\r\n", _mail->seq);
 
 		pmail->prefetching = TRUE;
-		pmail->imail.data.stream =
+		pmail->prefetch_stream =
 			pop3c_client_cmd_stream_async(mbox->client, cmd,
 				pop3c_mail_prefetch_done, pmail);
-		i_stream_set_name(pmail->imail.data.stream, t_strcut(cmd, '\r'));
+		i_stream_set_name(pmail->prefetch_stream, t_strcut(cmd, '\r'));
 		return !pmail->prefetching;
 	}
 	return index_mail_prefetch(_mail);
@@ -193,6 +199,11 @@
 		pop3c_client_wait_one(mbox->client);
 	}
 
+	if (pmail->prefetch_stream != NULL && mail->data.stream == NULL) {
+		mail->data.stream = pmail->prefetch_stream;
+		pmail->prefetch_stream = NULL;
+	}
+
 	if (get_body && mail->data.stream != NULL) {
 		name = i_stream_get_name(mail->data.stream);
 		if (strncmp(name, "RETR", 4) == 0) {
--- a/src/lib-storage/index/pop3c/pop3c-storage.h	Wed Jan 27 01:12:21 2016 +0200
+++ b/src/lib-storage/index/pop3c/pop3c-storage.h	Wed Jan 27 02:04:00 2016 +0200
@@ -31,6 +31,7 @@
 
 struct pop3c_mail {
 	struct index_mail imail;
+	struct istream *prefetch_stream;
 
 	unsigned int prefetching:1;
 	unsigned int prefetching_body:1;