changeset 20702:d02d6f19ff13

fs-api: Retrieve errno before dest->copy_output is NULL'ed CID 10144: When aborting output, errno retrieval is attempted after dest->copy_output has already been unreffed. Found by coverity.
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Mon, 05 Sep 2016 12:42:08 +0300
parents 05105c37062a
children 5ac29b54b474
files src/lib-fs/fs-api.c
diffstat 1 files changed, 4 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-fs/fs-api.c	Tue Sep 06 09:24:06 2016 +0300
+++ b/src/lib-fs/fs-api.c	Mon Sep 05 12:42:08 2016 +0300
@@ -888,6 +888,7 @@
 
 int fs_default_copy(struct fs_file *src, struct fs_file *dest)
 {
+	int tmp_errno;
 	/* we're going to be counting this as read+write, so remove the
 	   copy_count we just added */
 	dest->fs->stats.copy_count--;
@@ -917,11 +918,13 @@
 		return -1;
 	}
 	if (dest->copy_output->stream_errno != 0) {
+		/* errno might not survive abort error */
+		tmp_errno = dest->copy_output->stream_errno;
 		fs_write_stream_abort_error(dest, &dest->copy_output,
 					    "write(%s) failed: %s",
 					    o_stream_get_name(dest->copy_output),
 					    o_stream_get_error(dest->copy_output));
-		errno = dest->copy_output->stream_errno;
+		errno = tmp_errno;
 		i_stream_unref(&dest->copy_input);
 		return -1;
 	}