bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

af_alg: what if sendfile fails?


From: Bruno Haible
Subject: af_alg: what if sendfile fails?
Date: Mon, 25 Jun 2018 00:40:51 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-128-generic; KDE/5.18.0; x86_64; ; )

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  <address@hidden>

        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

[Prev in Thread] Current Thread [Next in Thread]