From 3929fb2098d350e73c9556f59c18d74cd45b0547 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 5 May 2018 11:08:08 -0700 Subject: [PATCH] af_alg: minor style improvements MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * lib/af_alg.c (afalg_stream): Prefer C99 style decl-after-statement, since we’re already assuming C99. Clarify by strengthening the bind test and omit unnecessary assignment. --- ChangeLog | 5 +++++ lib/af_alg.c | 26 +++++++++++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6704802f95..bdf18d44f1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,11 @@ 2018-05-05 Paul Eggert + af_alg: minor style improvements + * lib/af_alg.c (afalg_stream): Prefer C99 style + decl-after-statement, since we’re already assuming C99. Clarify + by strengthening the bind test and omit unnecessary assignment. + sys-limits.h: new file for crypto and safe I/O * lib/af_alg.c: Include sys-limits.h. (MAX_RW_COUNT): Remove. Use replaced by SYS_BUFSIZE_MAX. diff --git a/lib/af_alg.c b/lib/af_alg.c index 06344b1d60..519d6f8f3e 100644 --- a/lib/af_alg.c +++ b/lib/af_alg.c @@ -35,32 +35,29 @@ #define BLOCKSIZE 32768 int -afalg_stream (FILE * stream, const char *alg, void *resblock, ssize_t hashlen) +afalg_stream (FILE *stream, const char *alg, void *resblock, ssize_t hashlen) { + int cfd = socket (AF_ALG, SOCK_SEQPACKET, 0); + if (cfd < 0) + return -EAFNOSUPPORT; + struct sockaddr_alg salg = { .salg_family = AF_ALG, .salg_type = "hash", }; - int ret, cfd, ofd; - struct stat st; - - cfd = socket (AF_ALG, SOCK_SEQPACKET, 0); - if (cfd < 0) - return -EAFNOSUPPORT; - - /* avoid calling both strcpy and strlen. */ + /* Avoid calling both strcpy and strlen. */ for (int i = 0; (salg.salg_name[i] = alg[i]); i++) if (i == sizeof salg.salg_name - 1) return -EINVAL; - ret = bind (cfd, (struct sockaddr *) &salg, sizeof salg); - if (ret < 0) + int ret = bind (cfd, (struct sockaddr *) &salg, sizeof salg); + if (ret != 0) { ret = -EAFNOSUPPORT; goto out_cfd; } - ofd = accept (cfd, NULL, 0); + int ofd = accept (cfd, NULL, 0); if (ofd < 0) { ret = -EAFNOSUPPORT; @@ -68,7 +65,8 @@ afalg_stream (FILE * stream, const char *alg, void *resblock, ssize_t hashlen) } /* if file is a regular file, attempt sendfile to pipe the data. */ - if (!fstat (fileno (stream), &st) + struct stat st; + if (fstat (fileno (stream), &st) == 0 && (S_ISREG (st.st_mode) || S_TYPEISSHM (&st) || S_TYPEISTMO (&st)) && 0 < st.st_size && st.st_size <= SYS_BUFSIZE_MAX) { @@ -77,8 +75,6 @@ afalg_stream (FILE * stream, const char *alg, void *resblock, ssize_t hashlen) ret = -EIO; goto out_ofd; } - else - ret = 0; } else { -- 2.39.5