From b933013b65df63a6607253d1162c770c835c5e59 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Wed, 8 May 2019 14:17:22 +0200 Subject: crypto: digest: fix digesting file windows When digesting a file we always try toread PAGE_SIZE bytes. When we get a short read because we reached the file end then the code works correctly. If instead we only want to digest a part of the file then we must make sure to only read up to 'size' bytes. Fixes: b77582effd ("crypto: digest: Split memory vs. file code into separate functions") Signed-off-by: Sascha Hauer --- crypto/digest.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crypto/digest.c b/crypto/digest.c index 2c4de2e4f1..360c261e40 100644 --- a/crypto/digest.c +++ b/crypto/digest.c @@ -233,17 +233,18 @@ static int digest_update_from_fd(struct digest *d, int fd, } while (size) { - const ssize_t now = read(fd, buf, PAGE_SIZE); - if (now < 0) { - ret = now; + unsigned long now = min_t(typeof(size), PAGE_SIZE, size); + + ret = read(fd, buf, now); + if (ret < 0) { perror("read"); goto out_free; } - if (!now) + if (!ret) break; - ret = digest_update_interruptible(d, buf, now); + ret = digest_update_interruptible(d, buf, ret); if (ret) goto out_free; -- cgit v1.2.3