diff mbox series

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

Message ID 20200717034332.3726979-3-mail@aparcar.org
State Superseded
Headers show
Series [1/3] scripts: mkhash show -n option in usage | expand

Commit Message

Paul Spooren July 17, 2020, 3:43 a.m. UTC
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@aparcar.org>
---
 scripts/mkhash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Bjørn Mork July 17, 2020, 7:20 a.m. UTC | #1
Paul Spooren <mail@aparcar.org> writes:

> 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.

This prevents mkhash from working with read-only files.  That's
unexpected, and IMHO worse than the problem you are trying to fix.


Bjørn
Paul Spooren July 17, 2020, 8:22 a.m. UTC | #2
Hi,

On 16.07.20 21:20, Bjørn Mork wrote:
> Paul Spooren <mail@aparcar.org> writes:
>
>> 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.
> This prevents mkhash from working with read-only files.  That's
> unexpected, and IMHO worse than the problem you are trying to fix.

Thank you, I created a v2 using stat().
diff mbox series

Patch

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);