Paul Eggert
2018-05-10 01:31:48 UTC
* lib/af_alg.c (afalg_buffer): Fix typo I recently introduced.
(afalg_stream): Simplify and avoid the need for a runtime test
at the end.
---
ChangeLog | 5 +++++
lib/af_alg.c | 19 ++++++-------------
2 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 4eb095858..420426fe1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2018-05-09 Paul Eggert <***@cs.ucla.edu>
+ af_alg: fix my typo in afalg_buffer
+ * lib/af_alg.c (afalg_buffer): Fix typo I recently introduced.
+ (afalg_stream): Simplify and avoid the need for a runtime test
+ at the end.
+
af_alg: recover better from crypto failures
* lib/af_alg.c (afalg_stream): Recover from crypto failures if the
input stream is seekable, by repositioning the stream back to
diff --git a/lib/af_alg.c b/lib/af_alg.c
index 677a8e340..ded4b326b 100644
--- a/lib/af_alg.c
+++ b/lib/af_alg.c
@@ -86,7 +86,7 @@ afalg_buffer (const char *buffer, size_t len, const char *alg,
len -= size;
if (len == 0)
{
- result = read (ofd, resblock, hashlen) == hashlen ? -EAFNOSUPPORT: 0;
+ result = read (ofd, resblock, hashlen) == hashlen ? 0 : -EAFNOSUPPORT;
break;
}
}
@@ -108,7 +108,7 @@ afalg_stream (FILE *stream, const char *alg,
with /proc files that pretend to be empty, and lets the classic
read-write loop work around an empty-input bug noted below. */
int fd = fileno (stream);
- int result = 0;
+ int result;
struct stat st;
off_t nseek = 0, off = ftello (stream);
if (0 <= off && fstat (fd, &st) == 0
@@ -116,8 +116,7 @@ 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;
- if (sendfile (ofd, fd, &off, nbytes) != nbytes)
- result = -EAFNOSUPPORT;
+ result = sendfile (ofd, fd, &off, nbytes) == nbytes ? 0 : -EAFNOSUPPORT;
}
else
{
@@ -132,17 +131,14 @@ afalg_stream (FILE *stream, const char *alg,
{
/* On Linux < 4.9, the value for an empty stream is wrong (all 0).
See <https://patchwork.kernel.org/patch/9308641/>. */
- if (nseek == 0)
- result = -EAFNOSUPPORT;
-
- if (ferror (stream))
- result = -EIO;
+ result = ferror (stream) ? -EIO : nseek == 0 ? -EAFNOSUPPORT : 0;
break;
}
nseek -= size;
if (send (ofd, buf, size, MSG_MORE) != size)
{
- result = -EAFNOSUPPORT;
+ result = (fseeko (stream, nseek, SEEK_CUR) == 0
+ ? -EAFNOSUPPORT : -EIO);
break;
}
}
@@ -151,9 +147,6 @@ afalg_stream (FILE *stream, const char *alg,
if (result == 0 && read (ofd, resblock, hashlen) != hashlen)
result = -EAFNOSUPPORT;
close (ofd);
- if (result == -EAFNOSUPPORT && nseek != 0
- && fseeko (stream, nseek, SEEK_CUR) != 0)
- result = -EIO;
return result;
}
(afalg_stream): Simplify and avoid the need for a runtime test
at the end.
---
ChangeLog | 5 +++++
lib/af_alg.c | 19 ++++++-------------
2 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 4eb095858..420426fe1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2018-05-09 Paul Eggert <***@cs.ucla.edu>
+ af_alg: fix my typo in afalg_buffer
+ * lib/af_alg.c (afalg_buffer): Fix typo I recently introduced.
+ (afalg_stream): Simplify and avoid the need for a runtime test
+ at the end.
+
af_alg: recover better from crypto failures
* lib/af_alg.c (afalg_stream): Recover from crypto failures if the
input stream is seekable, by repositioning the stream back to
diff --git a/lib/af_alg.c b/lib/af_alg.c
index 677a8e340..ded4b326b 100644
--- a/lib/af_alg.c
+++ b/lib/af_alg.c
@@ -86,7 +86,7 @@ afalg_buffer (const char *buffer, size_t len, const char *alg,
len -= size;
if (len == 0)
{
- result = read (ofd, resblock, hashlen) == hashlen ? -EAFNOSUPPORT: 0;
+ result = read (ofd, resblock, hashlen) == hashlen ? 0 : -EAFNOSUPPORT;
break;
}
}
@@ -108,7 +108,7 @@ afalg_stream (FILE *stream, const char *alg,
with /proc files that pretend to be empty, and lets the classic
read-write loop work around an empty-input bug noted below. */
int fd = fileno (stream);
- int result = 0;
+ int result;
struct stat st;
off_t nseek = 0, off = ftello (stream);
if (0 <= off && fstat (fd, &st) == 0
@@ -116,8 +116,7 @@ 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;
- if (sendfile (ofd, fd, &off, nbytes) != nbytes)
- result = -EAFNOSUPPORT;
+ result = sendfile (ofd, fd, &off, nbytes) == nbytes ? 0 : -EAFNOSUPPORT;
}
else
{
@@ -132,17 +131,14 @@ afalg_stream (FILE *stream, const char *alg,
{
/* On Linux < 4.9, the value for an empty stream is wrong (all 0).
See <https://patchwork.kernel.org/patch/9308641/>. */
- if (nseek == 0)
- result = -EAFNOSUPPORT;
-
- if (ferror (stream))
- result = -EIO;
+ result = ferror (stream) ? -EIO : nseek == 0 ? -EAFNOSUPPORT : 0;
break;
}
nseek -= size;
if (send (ofd, buf, size, MSG_MORE) != size)
{
- result = -EAFNOSUPPORT;
+ result = (fseeko (stream, nseek, SEEK_CUR) == 0
+ ? -EAFNOSUPPORT : -EIO);
break;
}
}
@@ -151,9 +147,6 @@ afalg_stream (FILE *stream, const char *alg,
if (result == 0 && read (ofd, resblock, hashlen) != hashlen)
result = -EAFNOSUPPORT;
close (ofd);
- if (result == -EAFNOSUPPORT && nseek != 0
- && fseeko (stream, nseek, SEEK_CUR) != 0)
- result = -EIO;
return result;
}
--
2.17.0
2.17.0