From: Bruno Haible <bruno@clisp.org>
Date: Sat, 5 May 2018 18:02:58 +0000 (+0200)
Subject: af_alg: Fix bug on empty files.
X-Git-Tag: v1.0~5653
X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=ba847c6e7fc1da1e6921dcfef164a7efe2ee3b48;p=gnulib.git

af_alg: Fix bug on empty files.

* lib/af_alg.c (afalg_stream): Ignore the kernel's result if the input
stream is empty.
---

diff --git a/ChangeLog b/ChangeLog
index 0f65014692..6704802f95 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2018-05-05  Bruno Haible  <bruno@clisp.org>
+
+	af_alg: Fix bug on empty files.
+	* lib/af_alg.c (afalg_stream): Ignore the kernel's result if the input
+	stream is empty.
+
 2018-05-05  Paul Eggert  <eggert@cs.ucla.edu>
 
 	sys-limits.h: new file for crypto and safe I/O
diff --git a/lib/af_alg.c b/lib/af_alg.c
index 1f4a6aab51..06344b1d60 100644
--- a/lib/af_alg.c
+++ b/lib/af_alg.c
@@ -83,10 +83,12 @@ afalg_stream (FILE * stream, const char *alg, void *resblock, ssize_t hashlen)
   else
     {
       /* sendfile not possible, do a classic read-write loop.  */
+      int non_empty = 0;
       ssize_t size;
       char buf[BLOCKSIZE];
       while ((size = fread (buf, 1, sizeof buf, stream)))
         {
+          non_empty = 1;
           if (send (ofd, buf, size, MSG_MORE) != size)
             {
               ret = -EIO;
@@ -98,6 +100,13 @@ afalg_stream (FILE * stream, const char *alg, void *resblock, ssize_t hashlen)
           ret = -EIO;
           goto out_ofd;
         }
+      /* On Linux 4.4.0 at least, the value for an empty stream is wrong
+         (all zeroes).  */
+      if (!non_empty)
+        {
+          ret = -EAFNOSUPPORT;
+          goto out_ofd;
+        }
     }
 
   if (read (ofd, resblock, hashlen) != hashlen)