changeset 20:c45f17f58de1

Simplify key file reading
author Guido Berhoerster <guido+sencrypt@berhoerster.name>
date Wed, 31 Jul 2019 13:28:37 +0200
parents 0bd16b428fc0
children 5f9dc8f3c53e
files sencrypt.c
diffstat 1 files changed, 5 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/sencrypt.c	Wed Jul 31 11:21:59 2019 +0200
+++ b/sencrypt.c	Wed Jul 31 13:28:37 2019 +0200
@@ -69,7 +69,6 @@
 {
 	size_t		keyfile_size = 0;
 	FILE		*fp = NULL;
-	struct		stat statbuf;
 
 	fp = fopen(filename, "r");
 	if (fp == NULL) {
@@ -77,32 +76,15 @@
 		goto out;
 	}
 
-	if (fstat(fileno(fp), &statbuf) == -1) {
-		warn("could not stat key file \"%s\"", filename);
-		goto out;
-	}
-
-	if (!S_ISREG(statbuf.st_mode)) {
-		warnx("key file \"%s\" is not a regular file", filename);
+	keyfile_size = fread(key, 1, key_size_max, fp);
+	if (ferror(fp)) {
+		warn("failed to read key file \"%s\"", filename);
 		goto out;
-	}
-
-	if ((uintmax_t)statbuf.st_size > SIZE_MAX) {
-		warnx("key file \"%s\" is too large", filename);
-		goto out;
-	}
-	keyfile_size = (size_t)statbuf.st_size;
-	if ((keyfile_size > key_size_max) ||
-	    (keyfile_size == 0)) {
+	} else if (!feof(fp) || (keyfile_size < 1)) {
 		warnx("invalid key size");
 		goto out;
 	}
 
-	if (fread(key, 1, keyfile_size, fp) != keyfile_size) {
-		warnx("could not read key file \"%s\"", filename);
-		goto out;
-	}
-
 out:
 	if (fp != NULL) {
 		fclose(fp);
@@ -589,8 +571,7 @@
 	}
 
 	if (kflag) {
-		key_file_len = read_keyfile(key_filename, key,
-		    (off_t)sizeof (key));
+		key_file_len = read_keyfile(key_filename, key, sizeof (key));
 		if (key_file_len < 1) {
 			status = EXIT_FAILURE;
 			goto out;