In afalg_stream, if the stream refers to a regular file, and sendfile() fails,
do we have a guarantee that the offset of the file descriptor remains unchanged?

The man page at http://man7.org/linux/man-pages/man2/sendfile.2.html doesn't
say so.

How about this, then? Does this look right?


2018-06-24  Bruno Haible  <br...@clisp.org>

        af_alg: Fix error handling after sendfile() fails.
        * lib/af_alg.c (afalg_stream): Don't make assumptions about fd's
        position after a sendfile() failure.

diff --git a/lib/af_alg.c b/lib/af_alg.c
index 33d8837..3edcdf3 100644
--- a/lib/af_alg.c
+++ b/lib/af_alg.c
@@ -120,7 +120,11 @@ afalg_stream (FILE *stream, const char *alg,
       && off < st.st_size && st.st_size - off < SYS_BUFSIZE_MAX)
     {
       off_t nbytes = st.st_size - off;
-      result = sendfile (ofd, fd, &off, nbytes) == nbytes ? 0 : -EAFNOSUPPORT;
+      if (sendfile (ofd, fd, &off, nbytes) == nbytes)
+        result = 0;
+      else
+        /* Attempt to reposition fd to where it was.  */
+        result = (fseeko (stream, off, SEEK_SET) == 0 ? -EAFNOSUPPORT : -EIO);
     }
   else
     {


Reply via email to