[PATCH 3/3] scripts: mkhash fail on hashing a folder

Paul Spooren mail at aparcar.org
Thu Jul 16 23:43:32 EDT 2020


mkhash currently returns the hash of an empty input when trying to hash
a folder. This can be missleading in caseswhere e.g. an env variable is
undefined which should contain a filename. `mkhash ./path/to/$FILE`
would exit with code 0 and return a legit looking checksum.

A better behaviour would be to fail with exit code 1, which imitates the
behaviour of `md5sum` and `sha256sum`.

To avoid hashing of folders `fopen()` is called in `r+` mode which fails
on folders, as their are not writeable. Regular files work as before.

Hashing empty inputs result in the following checksums:
md5: d41d8cd98f00b204e9800998ecf8427e
sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Signed-off-by: Paul Spooren <mail at aparcar.org>
---
 scripts/mkhash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/mkhash.c b/scripts/mkhash.c
index 96f92e42b5..4d1ff1c166 100644
--- a/scripts/mkhash.c
+++ b/scripts/mkhash.c
@@ -770,7 +770,7 @@ static int hash_file(struct hash_type *t, const char *filename, bool add_filenam
 	if (!filename || !strcmp(filename, "-")) {
 		str = t->func(stdin);
 	} else {
-		FILE *f = fopen(filename, "r");
+		FILE *f = fopen(filename, "r+");
 
 		if (!f) {
 			fprintf(stderr, "Failed to open '%s'\n", filename);
-- 
2.25.1




More information about the openwrt-devel mailing list