14512a3
14512a3
# HG changeset patch
14512a3
# User Timo Sirainen <tss@iki.fi>
14512a3
# Date 1416386277 -32400
14512a3
# Node ID 80ed82a93c1af5f6885e75a34007eb52d0692a8d
14512a3
# Parent  31262a892ba7302bfea6e70e17d3acd468736d70
14512a3
mbox: Added more (and earlier) detection for errors in mbox istreams.
14512a3
14512a3
diff -r 31262a892ba7 -r 80ed82a93c1a src/lib-storage/index/mbox/istream-raw-mbox.c
14512a3
--- a/src/lib-storage/index/mbox/istream-raw-mbox.c	Wed Nov 19 17:37:18 2014 +0900
14512a3
+++ b/src/lib-storage/index/mbox/istream-raw-mbox.c	Wed Nov 19 17:37:57 2014 +0900
14512a3
@@ -506,7 +506,8 @@
14512a3
 	return rstream->from_offset;
14512a3
 }
14512a3
 
14512a3
-uoff_t istream_raw_mbox_get_header_offset(struct istream *stream)
14512a3
+int istream_raw_mbox_get_header_offset(struct istream *stream,
14512a3
+				       uoff_t *hdr_offset_r)
14512a3
 {
14512a3
 	struct raw_mbox_istream *rstream =
14512a3
 		(struct raw_mbox_istream *)stream->real_stream;
14512a3
@@ -520,13 +521,17 @@
14512a3
 		i_error("Unexpectedly lost From-line from mbox file %s at "
14512a3
 			"%"PRIuUOFF_T, i_stream_get_name(stream),
14512a3
 			rstream->from_offset);
14512a3
-		return (uoff_t)-1;
14512a3
+		return -1;
14512a3
 	}
14512a3
+	if (stream->stream_errno != 0)
14512a3
+		return -1;
14512a3
 
14512a3
-	return rstream->hdr_offset;
14512a3
+	*hdr_offset_r = rstream->hdr_offset;
14512a3
+	return 0;
14512a3
 }
14512a3
 
14512a3
-uoff_t istream_raw_mbox_get_body_offset(struct istream *stream)
14512a3
+int istream_raw_mbox_get_body_offset(struct istream *stream,
14512a3
+				     uoff_t *body_offset_r)
14512a3
 {
14512a3
 	struct raw_mbox_istream *rstream =
14512a3
 		(struct raw_mbox_istream *)stream->real_stream;
14512a3
@@ -534,8 +539,10 @@
14512a3
 
14512a3
 	i_assert(rstream->seeked);
14512a3
 
14512a3
-	if (rstream->body_offset != (uoff_t)-1)
14512a3
-		return rstream->body_offset;
14512a3
+	if (rstream->body_offset != (uoff_t)-1) {
14512a3
+		*body_offset_r = rstream->body_offset;
14512a3
+		return 0;
14512a3
+	}
14512a3
 
14512a3
 	offset = stream->v_offset;
14512a3
 	i_stream_seek(stream, rstream->hdr_offset);
14512a3
@@ -551,27 +558,30 @@
14512a3
 			} else {
14512a3
 				i_assert(rstream->body_offset != (uoff_t)-1);
14512a3
 			}
14512a3
-			break;
14512a3
+			return -1;
14512a3
 		}
14512a3
 	}
14512a3
 
14512a3
 	i_stream_seek(stream, offset);
14512a3
-	return rstream->body_offset;
14512a3
+	*body_offset_r = rstream->body_offset;
14512a3
+	return 0;
14512a3
 }
14512a3
 
14512a3
-uoff_t istream_raw_mbox_get_body_size(struct istream *stream,
14512a3
-				      uoff_t expected_body_size)
14512a3
+int istream_raw_mbox_get_body_size(struct istream *stream,
14512a3
+				   uoff_t expected_body_size,
14512a3
+				   uoff_t *body_size_r)
14512a3
 {
14512a3
 	struct raw_mbox_istream *rstream =
14512a3
 		(struct raw_mbox_istream *)stream->real_stream;
14512a3
 	const unsigned char *data;
14512a3
 	size_t size;
14512a3
-	uoff_t old_offset, body_size, next_body_offset;
14512a3
+	uoff_t old_offset, body_offset, body_size, next_body_offset;
14512a3
 
14512a3
 	i_assert(rstream->seeked);
14512a3
 	i_assert(rstream->hdr_offset != (uoff_t)-1);
14512a3
 
14512a3
-	(void)istream_raw_mbox_get_body_offset(stream);
14512a3
+	if (istream_raw_mbox_get_body_offset(stream, &body_offset) < 0)
14512a3
+		return -1;
14512a3
 	body_size = rstream->mail_size == (uoff_t)-1 ? (uoff_t)-1 :
14512a3
 		rstream->mail_size - (rstream->body_offset -
14512a3
 				      rstream->hdr_offset);
14512a3
@@ -580,8 +590,10 @@
14512a3
 		/* if we already have the existing body size, use it as long as
14512a3
 		   it's >= expected body_size. otherwise the previous parsing
14512a3
 		   may have stopped at a From_-line that belongs to the body. */
14512a3
-		if (body_size != (uoff_t)-1 && body_size >= expected_body_size)
14512a3
-			return body_size;
14512a3
+		if (body_size != (uoff_t)-1 && body_size >= expected_body_size) {
14512a3
+			*body_size_r = body_size;
14512a3
+			return 0;
14512a3
+		}
14512a3
 
14512a3
 		next_body_offset = rstream->body_offset + expected_body_size;
14512a3
 		/* If header_missing_eoh is set, the message body begins with
14512a3
@@ -600,21 +612,27 @@
14512a3
 			rstream->mail_size =
14512a3
 				next_body_offset - rstream->hdr_offset;
14512a3
 			i_stream_seek(stream, old_offset);
14512a3
-			return expected_body_size;
14512a3
+			*body_size_r = expected_body_size;
14512a3
+			return 0;
14512a3
 		}
14512a3
 		/* invalid expected_body_size */
14512a3
 	}
14512a3
-	if (body_size != (uoff_t)-1)
14512a3
-		return body_size;
14512a3
+	if (body_size != (uoff_t)-1) {
14512a3
+		*body_size_r = body_size;
14512a3
+		return 0;
14512a3
+	}
14512a3
 
14512a3
 	/* have to read through the message body */
14512a3
 	while (i_stream_read_data(stream, &data, &size, 0) > 0)
14512a3
 		i_stream_skip(stream, size);
14512a3
 	i_stream_seek(stream, old_offset);
14512a3
+	if (stream->stream_errno != 0)
14512a3
+		return -1;
14512a3
 
14512a3
 	i_assert(rstream->mail_size != (uoff_t)-1);
14512a3
-	return rstream->mail_size -
14512a3
+	*body_size_r = rstream->mail_size -
14512a3
 		(rstream->body_offset - rstream->hdr_offset);
14512a3
+	return 0;
14512a3
 }
14512a3
 
14512a3
 time_t istream_raw_mbox_get_received_time(struct istream *stream)
14512a3
@@ -651,13 +669,15 @@
14512a3
 	return rstream->crlf_ending;
14512a3
 }
14512a3
 
14512a3
-void istream_raw_mbox_next(struct istream *stream, uoff_t expected_body_size)
14512a3
+int istream_raw_mbox_next(struct istream *stream, uoff_t expected_body_size)
14512a3
 {
14512a3
 	struct raw_mbox_istream *rstream =
14512a3
 		(struct raw_mbox_istream *)stream->real_stream;
14512a3
 	uoff_t body_size;
14512a3
 
14512a3
-	body_size = istream_raw_mbox_get_body_size(stream, expected_body_size);
14512a3
+	if (istream_raw_mbox_get_body_size(stream, expected_body_size,
14512a3
+					   &body_size) < 0)
14512a3
+		return -1;
14512a3
 	rstream->mail_size = (uoff_t)-1;
14512a3
 
14512a3
 	rstream->received_time = rstream->next_received_time;
14512a3
@@ -678,6 +698,7 @@
14512a3
 
14512a3
 	rstream->eof = FALSE;
14512a3
 	rstream->istream.istream.eof = FALSE;
14512a3
+	return 0;
14512a3
 }
14512a3
 
14512a3
 int istream_raw_mbox_seek(struct istream *stream, uoff_t offset)
14512a3
@@ -698,10 +719,8 @@
14512a3
 	/* if seeked is FALSE, we unlocked in the middle. don't try to use
14512a3
 	   any cached state then. */
14512a3
 	if (rstream->mail_size != (uoff_t)-1 && rstream->seeked &&
14512a3
-	    rstream->hdr_offset + rstream->mail_size == offset) {
14512a3
-		istream_raw_mbox_next(stream, (uoff_t)-1);
14512a3
-		return 0;
14512a3
-	}
14512a3
+	    rstream->hdr_offset + rstream->mail_size == offset)
14512a3
+		return istream_raw_mbox_next(stream, (uoff_t)-1);
14512a3
 
14512a3
 	if (offset == rstream->from_offset && rstream->seeked) {
14512a3
 		/* back to beginning of current message */
14512a3
diff -r 31262a892ba7 -r 80ed82a93c1a src/lib-storage/index/mbox/istream-raw-mbox.h
14512a3
--- a/src/lib-storage/index/mbox/istream-raw-mbox.h	Wed Nov 19 17:37:18 2014 +0900
14512a3
+++ b/src/lib-storage/index/mbox/istream-raw-mbox.h	Wed Nov 19 17:37:57 2014 +0900
14512a3
@@ -9,15 +9,18 @@
14512a3
 /* Return offset to beginning of the "\nFrom"-line. */
14512a3
 uoff_t istream_raw_mbox_get_start_offset(struct istream *stream);
14512a3
 /* Return offset to beginning of the headers. */
14512a3
-uoff_t istream_raw_mbox_get_header_offset(struct istream *stream);
14512a3
+int istream_raw_mbox_get_header_offset(struct istream *stream,
14512a3
+				       uoff_t *hdr_offset_r);
14512a3
 /* Return offset to beginning of the body. */
14512a3
-uoff_t istream_raw_mbox_get_body_offset(struct istream *stream);
14512a3
+int istream_raw_mbox_get_body_offset(struct istream *stream,
14512a3
+				     uoff_t *body_offset_r);
14512a3
 
14512a3
 /* Return the number of bytes in the body of this message. If
14512a3
    expected_body_size isn't (uoff_t)-1, we'll use it as potentially valid body
14512a3
    size to avoid actually reading through the whole message. */
14512a3
-uoff_t istream_raw_mbox_get_body_size(struct istream *stream,
14512a3
-				      uoff_t expected_body_size);
14512a3
+int istream_raw_mbox_get_body_size(struct istream *stream,
14512a3
+				   uoff_t expected_body_size,
14512a3
+				   uoff_t *body_size_r);
14512a3
 
14512a3
 /* Return received time of current message, or (time_t)-1 if the timestamp is
14512a3
    broken. */
14512a3
@@ -30,7 +33,7 @@
14512a3
 
14512a3
 /* Jump to next message. If expected_body_size isn't (uoff_t)-1, we'll use it
14512a3
    as potentially valid body size. */
14512a3
-void istream_raw_mbox_next(struct istream *stream, uoff_t expected_body_size);
14512a3
+int istream_raw_mbox_next(struct istream *stream, uoff_t expected_body_size);
14512a3
 
14512a3
 /* Seek to message at given offset. offset must point to beginning of
14512a3
    "\nFrom ", or 0 for beginning of file. Returns -1 if it offset doesn't
14512a3
diff -r 31262a892ba7 -r 80ed82a93c1a src/lib-storage/index/mbox/mbox-mail.c
14512a3
--- a/src/lib-storage/index/mbox/mbox-mail.c	Wed Nov 19 17:37:18 2014 +0900
14512a3
+++ b/src/lib-storage/index/mbox/mbox-mail.c	Wed Nov 19 17:37:57 2014 +0900
14512a3
@@ -304,10 +304,10 @@
14512a3
 		return -1;
14512a3
 
14512a3
 	/* our header size varies, so don't do any caching */
14512a3
-	body_offset = istream_raw_mbox_get_body_offset(mbox->mbox_stream);
14512a3
-	if (body_offset == (uoff_t)-1) {
14512a3
+	if (istream_raw_mbox_get_body_offset(mbox->mbox_stream, &body_offset) < 0) {
14512a3
 		mail_storage_set_critical(_mail->box->storage,
14512a3
-					  "Couldn't get mbox size");
14512a3
+			"mbox %s: Couldn't get body offset for uid=%u",
14512a3
+			mailbox_get_path(&mbox->box), mail->mail.mail.uid);
14512a3
 		return -1;
14512a3
 	}
14512a3
 
14512a3
@@ -319,8 +319,13 @@
14512a3
 		body_size = (uoff_t)-1;
14512a3
 
14512a3
 	/* verify that the calculated body size is correct */
14512a3
-	body_size = istream_raw_mbox_get_body_size(mbox->mbox_stream,
14512a3
-						   body_size);
14512a3
+	if (istream_raw_mbox_get_body_size(mbox->mbox_stream,
14512a3
+					   body_size, &body_size) < 0) {
14512a3
+		mail_storage_set_critical(_mail->box->storage,
14512a3
+			"mbox %s: Couldn't get body size for uid=%u",
14512a3
+			mailbox_get_path(&mbox->box), mail->mail.mail.uid);
14512a3
+		return -1;
14512a3
+	}
14512a3
 
14512a3
 	data->physical_size = hdr_size.physical_size + body_size;
14512a3
 	*size_r = data->physical_size;
14512a3
@@ -352,7 +357,12 @@
14512a3
 	}
14512a3
 
14512a3
 	raw_stream = mbox->mbox_stream;
14512a3
-	hdr_offset = istream_raw_mbox_get_header_offset(raw_stream);
14512a3
+	if (istream_raw_mbox_get_header_offset(raw_stream, &hdr_offset) < 0) {
14512a3
+		mail_storage_set_critical(mbox->box.storage,
14512a3
+			"mbox %s: Couldn't get header offset for uid=%u",
14512a3
+			mailbox_get_path(&mbox->box), mail->mail.mail.uid);
14512a3
+		return -1;
14512a3
+	}
14512a3
 	i_stream_seek(raw_stream, hdr_offset);
14512a3
 
14512a3
 	if (next_offset != (uoff_t)-1)
14512a3
diff -r 31262a892ba7 -r 80ed82a93c1a src/lib-storage/index/mbox/mbox-sync-parse.c
14512a3
--- a/src/lib-storage/index/mbox/mbox-sync-parse.c	Wed Nov 19 17:37:18 2014 +0900
14512a3
+++ b/src/lib-storage/index/mbox/mbox-sync-parse.c	Wed Nov 19 17:37:57 2014 +0900
14512a3
@@ -453,8 +453,8 @@
14512a3
 	return strcasecmp(key, func->header);
14512a3
 }
14512a3
 
14512a3
-void mbox_sync_parse_next_mail(struct istream *input,
14512a3
-			       struct mbox_sync_mail_context *ctx)
14512a3
+int mbox_sync_parse_next_mail(struct istream *input,
14512a3
+			      struct mbox_sync_mail_context *ctx)
14512a3
 {
14512a3
 	struct mbox_sync_context *sync_ctx = ctx->sync_ctx;
14512a3
 	struct message_header_parser_ctx *hdr_ctx;
14512a3
@@ -545,6 +545,12 @@
14512a3
 	}
14512a3
 
14512a3
 	ctx->body_offset = input->v_offset;
14512a3
+	if (input->stream_errno != 0) {
14512a3
+		mbox_sync_set_critical(ctx->sync_ctx, "read(%s) failed: %s",
14512a3
+			i_stream_get_name(input), i_stream_get_error(input));
14512a3
+		return -1;
14512a3
+	}
14512a3
+	return 0;
14512a3
 }
14512a3
 
14512a3
 bool mbox_sync_parse_match_mail(struct mbox_mailbox *mbox,
14512a3
diff -r 31262a892ba7 -r 80ed82a93c1a src/lib-storage/index/mbox/mbox-sync-private.h
14512a3
--- a/src/lib-storage/index/mbox/mbox-sync-private.h	Wed Nov 19 17:37:18 2014 +0900
14512a3
+++ b/src/lib-storage/index/mbox/mbox-sync-private.h	Wed Nov 19 17:37:57 2014 +0900
14512a3
@@ -158,8 +158,8 @@
14512a3
 void mbox_sync_set_critical(struct mbox_sync_context *sync_ctx,
14512a3
 			    const char *fmt, ...) ATTR_FORMAT(2, 3);
14512a3
 
14512a3
-void mbox_sync_parse_next_mail(struct istream *input,
14512a3
-			       struct mbox_sync_mail_context *ctx);
14512a3
+int mbox_sync_parse_next_mail(struct istream *input,
14512a3
+			      struct mbox_sync_mail_context *ctx);
14512a3
 bool mbox_sync_parse_match_mail(struct mbox_mailbox *mbox,
14512a3
 				struct mail_index_view *view, uint32_t seq);
14512a3
 
14512a3
diff -r 31262a892ba7 -r 80ed82a93c1a src/lib-storage/index/mbox/mbox-sync-rewrite.c
14512a3
--- a/src/lib-storage/index/mbox/mbox-sync-rewrite.c	Wed Nov 19 17:37:18 2014 +0900
14512a3
+++ b/src/lib-storage/index/mbox/mbox-sync-rewrite.c	Wed Nov 19 17:37:57 2014 +0900
14512a3
@@ -318,11 +318,11 @@
14512a3
 	return 1;
14512a3
 }
14512a3
 
14512a3
-static void mbox_sync_read_next(struct mbox_sync_context *sync_ctx,
14512a3
-				struct mbox_sync_mail_context *mail_ctx,
14512a3
-				struct mbox_sync_mail *mails,
14512a3
-				uint32_t seq, uint32_t idx,
14512a3
-				uoff_t expunged_space)
14512a3
+static int mbox_sync_read_next(struct mbox_sync_context *sync_ctx,
14512a3
+			       struct mbox_sync_mail_context *mail_ctx,
14512a3
+			       struct mbox_sync_mail *mails,
14512a3
+			       uint32_t seq, uint32_t idx,
14512a3
+			       uoff_t expunged_space)
14512a3
 {
14512a3
 	unsigned int first_mail_expunge_extra;
14512a3
 	uint32_t orig_next_uid;
14512a3
@@ -332,8 +332,12 @@
14512a3
 	mail_ctx->seq = seq;
14512a3
 	mail_ctx->header = sync_ctx->header;
14512a3
 
14512a3
-	mail_ctx->mail.offset =
14512a3
-		istream_raw_mbox_get_header_offset(sync_ctx->input);
14512a3
+	if (istream_raw_mbox_get_header_offset(sync_ctx->input,
14512a3
+					       &mail_ctx->mail.offset) < 0) {
14512a3
+		mbox_sync_set_critical(sync_ctx,
14512a3
+			"Couldn't get header offset for seq=%u", seq);
14512a3
+		return -1;
14512a3
+	}
14512a3
 	mail_ctx->mail.body_size = mails[idx].body_size;
14512a3
 
14512a3
 	orig_next_uid = sync_ctx->next_uid;
14512a3
@@ -361,7 +365,8 @@
14512a3
 		mails[idx].from_offset += first_mail_expunge_extra;
14512a3
 	}
14512a3
 
14512a3
-	mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx);
14512a3
+	if (mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx) < 0)
14512a3
+		return -1;
14512a3
 	i_assert(mail_ctx->mail.pseudo == mails[idx].pseudo);
14512a3
 
14512a3
 	/* set next_uid back before updating the headers. this is important
14512a3
@@ -381,6 +386,7 @@
14512a3
 		if (mail_ctx->have_eoh)
14512a3
 			str_append_c(mail_ctx->header, '\n');
14512a3
 	}
14512a3
+	return 0;
14512a3
 }
14512a3
 
14512a3
 static int mbox_sync_read_and_move(struct mbox_sync_context *sync_ctx,
14512a3
@@ -398,8 +404,9 @@
14512a3
 		if (mbox_sync_seek(sync_ctx, mails[idx].from_offset) < 0)
14512a3
 			return -1;
14512a3
 
14512a3
-		mbox_sync_read_next(sync_ctx, &new_mail_ctx, mails, seq, idx,
14512a3
-				    expunged_space);
14512a3
+		if (mbox_sync_read_next(sync_ctx, &new_mail_ctx, mails, seq, idx,
14512a3
+					expunged_space) < 0)
14512a3
+			return -1;
14512a3
 		mail_ctx = &new_mail_ctx;
14512a3
 	} else {
14512a3
 		i_assert(seq == mail_ctx->seq);
14512a3
diff -r 31262a892ba7 -r 80ed82a93c1a src/lib-storage/index/mbox/mbox-sync.c
14512a3
--- a/src/lib-storage/index/mbox/mbox-sync.c	Wed Nov 19 17:37:18 2014 +0900
14512a3
+++ b/src/lib-storage/index/mbox/mbox-sync.c	Wed Nov 19 17:37:57 2014 +0900
14512a3
@@ -131,8 +131,10 @@
14512a3
 mbox_sync_read_next_mail(struct mbox_sync_context *sync_ctx,
14512a3
 			 struct mbox_sync_mail_context *mail_ctx)
14512a3
 {
14512a3
+	uoff_t offset;
14512a3
+
14512a3
 	/* get EOF */
14512a3
-	(void)istream_raw_mbox_get_header_offset(sync_ctx->input);
14512a3
+	(void)istream_raw_mbox_get_header_offset(sync_ctx->input, &offset);
14512a3
 	if (istream_raw_mbox_is_eof(sync_ctx->input))
14512a3
 		return 0;
14512a3
 
14512a3
@@ -144,19 +146,27 @@
14512a3
 
14512a3
 	mail_ctx->mail.from_offset =
14512a3
 		istream_raw_mbox_get_start_offset(sync_ctx->input);
14512a3
-	mail_ctx->mail.offset =
14512a3
-		istream_raw_mbox_get_header_offset(sync_ctx->input);
14512a3
+	if (istream_raw_mbox_get_header_offset(sync_ctx->input, &mail_ctx->mail.offset) < 0) {
14512a3
+		mbox_sync_set_critical(sync_ctx,
14512a3
+			"Couldn't get header offset for seq=%u", mail_ctx->seq);
14512a3
+		return -1;
14512a3
+	}
14512a3
 
14512a3
-	mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx);
14512a3
+	if (mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx) < 0)
14512a3
+		return -1;
14512a3
+	if (istream_raw_mbox_is_corrupted(sync_ctx->input))
14512a3
+		return -1;
14512a3
+
14512a3
 	i_assert(sync_ctx->input->v_offset != mail_ctx->mail.from_offset ||
14512a3
 		 sync_ctx->input->eof);
14512a3
 
14512a3
-	if (istream_raw_mbox_is_corrupted(sync_ctx->input))
14512a3
+	if (istream_raw_mbox_get_body_size(sync_ctx->input,
14512a3
+					   mail_ctx->content_length,
14512a3
+					   &mail_ctx->mail.body_size) < 0) {
14512a3
+		mbox_sync_set_critical(sync_ctx,
14512a3
+			"Couldn't get body size for seq=%u", mail_ctx->seq);
14512a3
 		return -1;
14512a3
-
14512a3
-	mail_ctx->mail.body_size =
14512a3
-		istream_raw_mbox_get_body_size(sync_ctx->input,
14512a3
-					       mail_ctx->content_length);
14512a3
+	}
14512a3
 	i_assert(mail_ctx->mail.body_size < OFF_T_MAX);
14512a3
 
14512a3
 	if ((mail_ctx->mail.flags & MAIL_RECENT) != 0 &&
14512a3
@@ -810,7 +820,7 @@
14512a3
 mbox_sync_seek_to_seq(struct mbox_sync_context *sync_ctx, uint32_t seq)
14512a3
 {
14512a3
 	struct mbox_mailbox *mbox = sync_ctx->mbox;
14512a3
-	uoff_t old_offset;
14512a3
+	uoff_t old_offset, offset;
14512a3
 	uint32_t uid;
14512a3
 	int ret;
14512a3
         bool deleted;
14512a3
@@ -864,7 +874,11 @@
14512a3
 
14512a3
         sync_ctx->idx_seq = seq;
14512a3
 	sync_ctx->dest_first_mail = sync_ctx->seq == 0;
14512a3
-        (void)istream_raw_mbox_get_body_offset(sync_ctx->input);
14512a3
+	if (istream_raw_mbox_get_body_offset(sync_ctx->input, &offset) < 0) {
14512a3
+		mbox_sync_set_critical(sync_ctx,
14512a3
+			"Message body offset lookup failed");
14512a3
+		return -1;
14512a3
+	}
14512a3
 	return 1;
14512a3
 }
14512a3
 
14512a3
@@ -1149,8 +1163,9 @@
14512a3
 			sync_ctx->idx_seq++;
14512a3
 		}
14512a3
 
14512a3
-		istream_raw_mbox_next(sync_ctx->input,
14512a3
-				      mail_ctx->mail.body_size);
14512a3
+		if (istream_raw_mbox_next(sync_ctx->input,
14512a3
+					  mail_ctx->mail.body_size) < 0)
14512a3
+			return -1;
14512a3
 		offset = istream_raw_mbox_get_start_offset(sync_ctx->input);
14512a3
 
14512a3
 		if (sync_ctx->need_space_seq != 0) {
14512a3