From: Timo Sirainen Date: Thu, 12 Mar 2020 10:57:41 +0000 (+0200) Subject: lib: safe_sendfile() - clarify return value 0 handling X-Git-Tag: 2.3.11.2~509 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f0a845736edf78c322630da24ba42c6571654787;p=thirdparty%2Fdovecot%2Fcore.git lib: safe_sendfile() - clarify return value 0 handling --- diff --git a/src/lib/sendfile-util.c b/src/lib/sendfile-util.c index 83e420af3f..662285b14d 100644 --- a/src/lib/sendfile-util.c +++ b/src/lib/sendfile-util.c @@ -22,8 +22,7 @@ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count) off_t safe_offset; ssize_t ret; - if (count == 0) - return 0; + i_assert(count > 0); /* make sure given offset fits into off_t */ if (sizeof(off_t) * CHAR_BIT == 32) { @@ -49,7 +48,7 @@ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count) safe_offset = (off_t)*offset; ret = sendfile(out_fd, in_fd, &safe_offset, count); - /* ret=0 : trying to read past EOF, errno = EPIPE : remote is gone */ + /* ret=0 : trying to read past EOF */ *offset = (uoff_t)safe_offset; return ret; } @@ -65,14 +64,11 @@ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count) off_t sbytes; int ret; + /* if count=0 is passed to sendfile(), it sends everything + from in_fd until EOF. We don't want that. */ + i_assert(count > 0); i_assert(count <= SSIZE_T_MAX); - if (count == 0) { - /* if count=0 is passed to sendfile(), it sends everything - from in_fd until EOF. We don't want that. */ - return 0; - } - i_zero(&hdtr); ret = sendfile(in_fd, out_fd, *offset, count, &hdtr, &sbytes, 0); @@ -100,11 +96,9 @@ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count) ssize_t ret; off_t s_offset; + i_assert(count > 0); i_assert(count <= SSIZE_T_MAX); - if (count == 0) - return 0; - /* NOTE: if outfd is not a socket, some Solaris versions will kernel panic */ diff --git a/src/lib/sendfile-util.h b/src/lib/sendfile-util.h index 29ada6f605..3d7f14aa0a 100644 --- a/src/lib/sendfile-util.h +++ b/src/lib/sendfile-util.h @@ -1,9 +1,16 @@ #ifndef SENDFILE_UTIL_H #define SENDFILE_UTIL_H -/* Wrapper for various sendfile()-like calls. Returns -1 and errno=EINVAL if - it isn't supported for some reason (out_fd isn't a socket, offset is too - large, or there simply is no sendfile()). */ +/* Wrapper for various sendfile()-like calls. Read a maximum of count bytes + from the given offset in in_fd and write them to out_fd. The offset is + updated after the call. Note the call assert-crashes if count is 0. + + Returns: + >0 number of bytes successfully written (maybe less than count) + 0 if offset points to the input's EOF or past it + -1, errno=EINVAL if it isn't supported for some reason (out_fd isn't a + socket or there simply is no sendfile()). + -1, errno=EAGAIN if non-blocking write couldn't send anything */ ssize_t safe_sendfile(int out_fd, int in_fd, uoff_t *offset, size_t count); #endif