From bc4d023a3810a7da6ede2cc522cdc34fa3129378 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Fri, 25 Jan 2019 14:34:13 +0200 Subject: Improve error checking in stream writing functions. * libmailutils/stream/stream.c (mu_stream_write): Return EIO on short writes if pnwritten is NULL. * libmailutils/stream/streamcpy.c (mu_stream_copy): Likewise (if source stream size is known. --- libmailutils/stream/stream.c | 5 +++++ libmailutils/stream/streamcpy.c | 43 +++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/libmailutils/stream/stream.c b/libmailutils/stream/stream.c index de995aa57..8c9cbb6e0 100644 --- a/libmailutils/stream/stream.c +++ b/libmailutils/stream/stream.c @@ -1030,6 +1030,11 @@ mu_stream_write (mu_stream_t stream, const void *buf, size_t size, } if (pnwritten) *pnwritten = nbytes; + else if (nbytes < size) + { + rc = EIO; + mu_stream_seterr (stream, rc, 1); + } } return rc; } diff --git a/libmailutils/stream/streamcpy.c b/libmailutils/stream/streamcpy.c index 3270ee9fb..aaf0e7273 100644 --- a/libmailutils/stream/streamcpy.c +++ b/libmailutils/stream/streamcpy.c @@ -38,7 +38,7 @@ mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size, size_t bufsize, n; char *buf; mu_off_t total = 0; - + if (pcsz) *pcsz = 0; if (size == 0) @@ -56,7 +56,7 @@ mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size, default: return status; } - + if (size) { mu_off_t pos; @@ -68,7 +68,7 @@ mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size, return ESPIPE; size -= pos; break; - + case EACCES: mu_stream_clearerr (src); case ENOSYS: @@ -90,21 +90,26 @@ mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size, return ENOMEM; if (size) - while (size) - { - size_t rdsize = bufsize < size ? bufsize : size; + { + while (size) + { + size_t rdsize = bufsize < size ? bufsize : size; - status = mu_stream_read (src, buf, rdsize, &n); - if (status) - break; - if (n == 0) - break; - status = mu_stream_write (dst, buf, n, NULL); - if (status) - break; - size -= n; - total += n; - } + status = mu_stream_read (src, buf, rdsize, &n); + if (status) + break; + if (n == 0) + break; + status = mu_stream_write (dst, buf, n, NULL); + if (status) + break; + size -= n; + total += n; + } + + if (!pcsz && size) + status = EIO; + } else while ((status = mu_stream_read (src, buf, bufsize, &n)) == 0 && n > 0) @@ -124,7 +129,3 @@ mu_stream_copy (mu_stream_t dst, mu_stream_t src, mu_off_t size, free (buf); return status; } - - - - -- cgit v1.2.1