changeset 20828:d7bed2a85e99

global: Make sure i_stream_read() calls handle 0 and -2 return values correctly.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 06 Oct 2016 13:50:59 +0300
parents 7691ad109879
children 5e91b21a1a04
files src/doveadm/client-connection-http.c src/doveadm/doveadm-mail-save.c src/doveadm/doveadm-mail.c src/doveadm/dsync/dsync-mail.c src/imap/cmd-append.c src/lib-compression/istream-bzlib.c src/lib-compression/istream-lz4.c src/lib-compression/istream-lzma.c src/lib-compression/istream-zlib.c src/lib-dict-extra/dict-fs.c src/lib-http/http-client-connection.c src/lib-mail/istream-attachment-extractor.c src/lib-storage/index/index-attachment.c src/lib-storage/index/mbox/mbox-save.c src/lib-storage/mail-copy.c src/lib/istream-seekable.c
diffstat 16 files changed, 61 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/src/doveadm/client-connection-http.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/doveadm/client-connection-http.c	Thu Oct 06 13:50:59 2016 +0300
@@ -206,8 +206,11 @@
 
 static int doveadm_http_server_istream_read(struct client_connection_http *conn)
 {
-	while (i_stream_read(conn->cmd_param->value.v_istream) > 0)
-		i_stream_skip(conn->cmd_param->value.v_istream, i_stream_get_data_size(conn->cmd_param->value.v_istream));
+	const unsigned char *data;
+	size_t size;
+
+	while (i_stream_read_more(conn->cmd_param->value.v_istream, &data, &size) > 0)
+		i_stream_skip(conn->cmd_param->value.v_istream, size);
 	if (!conn->cmd_param->value.v_istream->eof)
 		return 0;
 
--- a/src/doveadm/doveadm-mail-save.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/doveadm/doveadm-mail-save.c	Thu Oct 06 13:50:59 2016 +0300
@@ -35,13 +35,13 @@
 		mailbox_transaction_rollback(&trans);
 		return -1;
 	}
-	while ((ret = i_stream_read(input)) > 0 || ret == -2) {
+	do {
 		if (mailbox_save_continue(save_ctx) < 0) {
 			save_failed = TRUE;
 			ret = -1;
 			break;
 		}
-	}
+	} while ((ret = i_stream_read(input)) > 0);
 	i_assert(ret == -1);
 
 	if (input->stream_errno != 0) {
--- a/src/doveadm/doveadm-mail.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/doveadm/doveadm-mail.c	Thu Oct 06 13:50:59 2016 +0300
@@ -163,8 +163,11 @@
 
 static void doveadm_mail_cmd_input_input(struct doveadm_mail_cmd_context *ctx)
 {
-	while (i_stream_read(ctx->cmd_input) > 0)
-		i_stream_skip(ctx->cmd_input, i_stream_get_data_size(ctx->cmd_input));
+	const unsigned char *data;
+	size_t size;
+
+	while (i_stream_read_more(ctx->cmd_input, &data, &size) > 0)
+		i_stream_skip(ctx->cmd_input, size);
 	if (!ctx->cmd_input->eof)
 		return;
 
--- a/src/doveadm/dsync/dsync-mail.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/doveadm/dsync/dsync-mail.c	Thu Oct 06 13:50:59 2016 +0300
@@ -35,6 +35,7 @@
 	unsigned char md5_result[MD5_RESULTLEN];
 	const unsigned char *data;
 	size_t size;
+	ssize_t sret;
 	int ret = 0;
 
 	hdr_ctx = mailbox_header_lookup_init(mail->box, hashed_headers);
@@ -47,15 +48,12 @@
 
 	md5_init(&md5_ctx);
 	memset(&hash_ctx, 0, sizeof(hash_ctx));
-	while (!i_stream_is_eof(input)) {
-		if (i_stream_read_data(input, &data, &size, 0) == -1)
-			break;
-		if (size == 0)
-			break;
+	while ((sret = i_stream_read_more(input, &data, &size)) > 0) {
 		message_header_hash_more(&hash_ctx, &hash_method_md5, &md5_ctx,
 					 version, data, size);
 		i_stream_skip(input, size);
 	}
+	i_assert(sret == -1);
 	if (input->stream_errno != 0)
 		ret = -1;
 	i_stream_unref(&input);
--- a/src/imap/cmd-append.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/imap/cmd-append.c	Thu Oct 06 13:50:59 2016 +0300
@@ -205,12 +205,10 @@
 	/* add this input stream to chain */
 	i_stream_chain_append(ctx->catchain, mpresult.input);
 	/* save by reading the chain stream */
-	while (!i_stream_is_eof(mpresult.input)) {
+	do {
 		ret = i_stream_read(mpresult.input);
 		i_assert(ret != 0); /* we can handle only blocking input here */
-		if (mailbox_save_continue(ctx->save_ctx) < 0 || ret == -1)
-			break;
-	}
+	} while (mailbox_save_continue(ctx->save_ctx) == 0 && ret != -1);
 
 	if (mpresult.input->stream_errno != 0) {
 		errno = mpresult.input->stream_errno;
--- a/src/lib-compression/istream-bzlib.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-compression/istream-bzlib.c	Thu Oct 06 13:50:59 2016 +0300
@@ -226,6 +226,8 @@
 		stream->pos = stream->skip;
 	} else {
 		/* read and cache forward */
+		ssize_t ret;
+
 		do {
 			size_t avail = stream->pos - stream->skip;
 
@@ -237,7 +239,8 @@
 			}
 
 			i_stream_skip(&stream->istream, avail);
-		} while (i_stream_read(&stream->istream) >= 0);
+		} while ((ret = i_stream_read(&stream->istream)) > 0);
+		i_assert(ret == -1);
 
 		if (stream->istream.v_offset != v_offset) {
 			/* some failure, we've broken it */
@@ -280,11 +283,13 @@
 
 	if (zstream->stream_size == (uoff_t)-1) {
 		uoff_t old_offset = stream->istream.v_offset;
+		ssize_t ret;
 
 		do {
 			size = i_stream_get_data_size(&stream->istream);
 			i_stream_skip(&stream->istream, size);
-		} while (i_stream_read(&stream->istream) > 0);
+		} while ((ret = i_stream_read(&stream->istream)) > 0);
+		i_assert(ret == -1);
 
 		i_stream_seek(&stream->istream, old_offset);
 		if (zstream->stream_size == (uoff_t)-1)
--- a/src/lib-compression/istream-lz4.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-compression/istream-lz4.c	Thu Oct 06 13:50:59 2016 +0300
@@ -210,6 +210,8 @@
 		stream->pos = stream->skip;
 	} else {
 		/* read and cache forward */
+		ssize_t ret;
+
 		do {
 			size_t avail = stream->pos - stream->skip;
 
@@ -221,7 +223,8 @@
 			}
 
 			i_stream_skip(&stream->istream, avail);
-		} while (i_stream_read(&stream->istream) >= 0);
+		} while ((ret = i_stream_read(&stream->istream)) > 0);
+		i_assert(ret == -1);
 
 		if (stream->istream.v_offset != v_offset) {
 			/* some failure, we've broken it */
@@ -264,11 +267,13 @@
 
 	if (zstream->stream_size == (uoff_t)-1) {
 		uoff_t old_offset = stream->istream.v_offset;
+		ssize_t ret;
 
 		do {
 			size = i_stream_get_data_size(&stream->istream);
 			i_stream_skip(&stream->istream, size);
-		} while (i_stream_read(&stream->istream) > 0);
+		} while ((ret = i_stream_read(&stream->istream)) > 0);
+		i_assert(ret == -1);
 
 		i_stream_seek(&stream->istream, old_offset);
 		if (zstream->stream_size == (uoff_t)-1)
--- a/src/lib-compression/istream-lzma.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-compression/istream-lzma.c	Thu Oct 06 13:50:59 2016 +0300
@@ -235,6 +235,8 @@
 		stream->pos = stream->skip;
 	} else {
 		/* read and cache forward */
+		ssize_t ret;
+
 		do {
 			size_t avail = stream->pos - stream->skip;
 
@@ -246,7 +248,8 @@
 			}
 
 			i_stream_skip(&stream->istream, avail);
-		} while (i_stream_read(&stream->istream) >= 0);
+		} while ((ret = i_stream_read(&stream->istream)) > 0);
+		i_assert(ret == -1);
 
 		if (stream->istream.v_offset != v_offset) {
 			/* some failure, we've broken it */
@@ -289,11 +292,13 @@
 
 	if (zstream->stream_size == (uoff_t)-1) {
 		uoff_t old_offset = stream->istream.v_offset;
+		ssize_t ret;
 
 		do {
 			size = i_stream_get_data_size(&stream->istream);
 			i_stream_skip(&stream->istream, size);
-		} while (i_stream_read(&stream->istream) > 0);
+		} while ((ret = i_stream_read(&stream->istream)) > 0);
+		i_assert(ret == -1);
 
 		i_stream_seek(&stream->istream, old_offset);
 		if (zstream->stream_size == (uoff_t)-1)
--- a/src/lib-compression/istream-zlib.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-compression/istream-zlib.c	Thu Oct 06 13:50:59 2016 +0300
@@ -379,6 +379,8 @@
 		stream->pos = stream->skip;
 	} else {
 		/* read and cache forward */
+		ssize_t ret;
+
 		do {
 			size_t avail = stream->pos - stream->skip;
 
@@ -390,7 +392,8 @@
 			}
 
 			i_stream_skip(&stream->istream, avail);
-		} while (i_stream_read(&stream->istream) >= 0);
+		} while ((ret = i_stream_read(&stream->istream)) > 0);
+		i_assert(ret == -1);
 
 		if (stream->istream.v_offset != v_offset) {
 			/* some failure, we've broken it */
@@ -433,11 +436,13 @@
 
 	if (zstream->stream_size == (uoff_t)-1) {
 		uoff_t old_offset = stream->istream.v_offset;
+		ssize_t ret;
 
 		do {
 			size = i_stream_get_data_size(&stream->istream);
 			i_stream_skip(&stream->istream, size);
-		} while (i_stream_read(&stream->istream) > 0);
+		} while ((ret = i_stream_read(&stream->istream)) > 0);
+		i_assert(ret == -1);
 
 		i_stream_seek(&stream->istream, old_offset);
 		if (zstream->stream_size == (uoff_t)-1)
--- a/src/lib-dict-extra/dict-fs.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-dict-extra/dict-fs.c	Thu Oct 06 13:50:59 2016 +0300
@@ -93,7 +93,7 @@
 	file = fs_file_init(dict->fs, fs_dict_get_full_key(dict, key),
 			    FS_OPEN_MODE_READONLY);
 	input = fs_read_stream(file, IO_BLOCK_SIZE);
-	i_stream_read(input);
+	(void)i_stream_read(input);
 
 	str = str_new(pool, i_stream_get_data_size(input)+1);
 	while ((ret = i_stream_read_data(input, &data, &size, 0)) > 0) {
--- a/src/lib-http/http-client-connection.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-http/http-client-connection.c	Thu Oct 06 13:50:59 2016 +0300
@@ -703,7 +703,7 @@
 	if (conn->ssl_iostream != NULL &&
 		!ssl_iostream_is_handshaked(conn->ssl_iostream)) {
 		/* finish SSL negotiation by reading from input stream */
-		while ((ret=i_stream_read(conn->conn.input)) > 0) {
+		while ((ret=i_stream_read(conn->conn.input)) > 0 || ret == -2) {
 			if (ssl_iostream_is_handshaked(conn->ssl_iostream))
 				break;
 		}
--- a/src/lib-mail/istream-attachment-extractor.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-mail/istream-attachment-extractor.c	Thu Oct 06 13:50:59 2016 +0300
@@ -375,8 +375,7 @@
 	o_stream_cork(output);
 
 	hash_format_reset(astream->set.hash_format);
-	while ((ret = i_stream_read(base64_input)) > 0) {
-		data = i_stream_get_data(base64_input, &size);
+	while ((ret = i_stream_read_more(base64_input, &data, &size)) > 0) {
 		buffer_set_used_size(buf, 0);
 		if (base64_decode(data, size, &size, buf) < 0) {
 			i_error("istream-attachment: BUG: "
--- a/src/lib-storage/index/index-attachment.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-storage/index/index-attachment.c	Thu Oct 06 13:50:59 2016 +0300
@@ -216,7 +216,7 @@
 
 	do {
 		ret = i_stream_read(attach->input);
-		if (ret > 0) {
+		if (ret > 0 || ret == -2) {
 			data = i_stream_get_data(attach->input, &size);
 			o_stream_nsend(ctx->data.output, data, size);
 			i_stream_skip(attach->input, size);
--- a/src/lib-storage/index/mbox/mbox-save.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-storage/index/mbox/mbox-save.c	Thu Oct 06 13:50:59 2016 +0300
@@ -598,8 +598,7 @@
 		return mbox_save_body(ctx);
 	}
 
-	while ((ret = i_stream_read(ctx->input)) > 0) {
-		data = i_stream_get_data(ctx->input, &size);
+	while ((ret = i_stream_read_more(ctx->input, &data, &size)) > 0) {
 		for (i = 0; i < size; i++) {
 			if (data[i] == '\n' &&
 			    ((i == 0 && ctx->last_char == '\n') ||
--- a/src/lib-storage/mail-copy.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib-storage/mail-copy.c	Thu Oct 06 13:50:59 2016 +0300
@@ -76,10 +76,13 @@
 	if (mailbox_save_begin(_ctx, input) < 0)
 		return -1;
 
+	ssize_t ret;
 	do {
 		if (mailbox_save_continue(ctx) < 0)
 			break;
-	} while (i_stream_read(input) != -1);
+		ret = i_stream_read(input);
+		i_assert(ret != 0);
+	} while (ret != -1);
 
 	if (input->stream_errno != 0) {
 		mail_storage_set_critical(ctx->transaction->box->storage,
--- a/src/lib/istream-seekable.c	Thu Oct 06 13:39:21 2016 +0300
+++ b/src/lib/istream-seekable.c	Thu Oct 06 13:50:59 2016 +0300
@@ -115,10 +115,14 @@
 		if (size >= stream->pos)
 			break;
 
-		if (i_stream_read(sstream->fd_input) <= 0) {
+		ssize_t ret;
+		if ((ret = i_stream_read(sstream->fd_input)) <= 0) {
+			i_assert(ret != 0);
 			i_error("istream-seekable: Couldn't read back "
-				"in-memory input %s",
-				i_stream_get_name(&stream->istream));
+				"in-memory input %s: %s",
+				i_stream_get_name(&stream->istream),
+				ret == -2 ? "buffer full" :
+				i_stream_get_error(sstream->fd_input));
 			i_stream_destroy(&sstream->fd_input);
 			return -1;
 		}