logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: 1f8bb7cd28421f75fbacba6d691a3ff98054c450
parent 0320e0b9c5421835fb612a6dc25fd455827997d0
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 23 Aug 2025 03:39:22 +0200

cmd/sha{1,256,512}sum: in -c mode, handle missing filename entry

Diffstat:

Mcmd/sha1sum.c39++++++++++++++++++++++++++-------------
Mcmd/sha256sum.c39++++++++++++++++++++++++++-------------
Mcmd/sha512sum.c39++++++++++++++++++++++++++-------------
Mtest-cmd/sha1sum.sh8++++----
Mtest-cmd/sha256sum.sh8++++----
Mtest-cmd/sha512sum.sh8++++----
6 files changed, 90 insertions(+), 51 deletions(-)

diff --git a/cmd/sha1sum.c b/cmd/sha1sum.c @@ -68,16 +68,27 @@ check(FILE *file, const char *filename) { if(line[nread - 1] == '\n') line[nread - 1] = '\0'; + // Checksum + 2 characters + filename +#define SHA1SUM_MIN_LINE_LEN SHA1SUM_LEN + 3 + if(nread < SHA1SUM_MIN_LINE_LEN) + { + fprintf(stderr, + "%s: error: Expected at least %zd characters, got %zd for line: %s\n", + argv0, + (size_t)SHA1SUM_MIN_LINE_LEN, + nread, + line); + if(len > 0) free(line); + return -1; + } + ssize_t i = 0; for(; i < nread; i++) { if(isxdigit(line[i])) continue; - if(line[i] == ' ') - { - line[i] = '\0'; - break; - } + if(line[i] == ' ') line[i] = '\0'; + if(line[i] == '\0') break; fprintf(stderr, "%s: error: Invalid character '%c' while reading hash in line: %s\n", @@ -87,23 +98,25 @@ check(FILE *file, const char *filename) if(len > 0) free(line); return -1; } - if(line[i++] != '\0') - { - fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); - if(len > 0) free(line); - return -1; - } - if(i != SHA1SUM_LEN) + if(i != SHA1SUM_LEN - 1) { fprintf(stderr, "%s: error: Got %zd hexadecimal digits while expected %d for a SHA1\n", argv0, i, - SHA1SUM_LEN); + SHA1SUM_LEN - 1); + if(len > 0) free(line); + return -1; + } + + if(line[i] != '\0') + { + fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); if(len > 0) free(line); return -1; } + if(i < nread) i++; while(i < nread && line[i] == ' ') i++; diff --git a/cmd/sha256sum.c b/cmd/sha256sum.c @@ -68,16 +68,27 @@ check(FILE *file, const char *filename) { if(line[nread - 1] == '\n') line[nread - 1] = '\0'; + // Checksum + 2 characters + filename +#define SHA256SUM_MIN_LINE_LEN SHA256SUM_LEN + 3 + if(nread < SHA256SUM_MIN_LINE_LEN) + { + fprintf(stderr, + "%s: error: Expected at least %zd characters, got %zd for line: %s\n", + argv0, + (size_t)SHA256SUM_MIN_LINE_LEN, + nread, + line); + if(len > 0) free(line); + return -1; + } + ssize_t i = 0; for(; i < nread; i++) { if(isxdigit(line[i])) continue; - if(line[i] == ' ') - { - line[i] = '\0'; - break; - } + if(line[i] == ' ') line[i] = '\0'; + if(line[i] == '\0') break; fprintf(stderr, "%s: error: Invalid character '%c' while reading hash in line: %s\n", @@ -87,23 +98,25 @@ check(FILE *file, const char *filename) if(len > 0) free(line); return -1; } - if(line[i++] != '\0') - { - fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); - if(len > 0) free(line); - return -1; - } - if(i != SHA256SUM_LEN) + if(i != SHA256SUM_LEN - 1) { fprintf(stderr, "%s: error: Got %zd hexadecimal digits while expected %d for a SHA256\n", argv0, i, - SHA256SUM_LEN); + SHA256SUM_LEN - 1); + if(len > 0) free(line); + return -1; + } + + if(line[i] != '\0') + { + fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); if(len > 0) free(line); return -1; } + if(i < nread) i++; while(i < nread && line[i] == ' ') i++; diff --git a/cmd/sha512sum.c b/cmd/sha512sum.c @@ -68,16 +68,27 @@ check(FILE *file, const char *filename) { if(line[nread - 1] == '\n') line[nread - 1] = '\0'; + // Checksum + 2 characters + filename +#define SHA512SUM_MIN_LINE_LEN SHA512SUM_LEN + 3 + if(nread < SHA512SUM_MIN_LINE_LEN) + { + fprintf(stderr, + "%s: error: Expected at least %zd characters, got %zd for line: %s\n", + argv0, + (size_t)SHA512SUM_MIN_LINE_LEN, + nread, + line); + if(len > 0) free(line); + return -1; + } + ssize_t i = 0; for(; i < nread; i++) { if(isxdigit(line[i])) continue; - if(line[i] == ' ') - { - line[i] = '\0'; - break; - } + if(line[i] == ' ') line[i] = '\0'; + if(line[i] == '\0') break; fprintf(stderr, "%s: error: Invalid character '%c' while reading hash in line: %s\n", @@ -87,23 +98,25 @@ check(FILE *file, const char *filename) if(len > 0) free(line); return -1; } - if(line[i++] != '\0') - { - fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); - if(len > 0) free(line); - return -1; - } - if(i != SHA512SUM_LEN) + if(i != SHA512SUM_LEN - 1) { fprintf(stderr, "%s: error: Got %zd hexadecimal digits while expected %d for a SHA512\n", argv0, i, - SHA512SUM_LEN); + SHA512SUM_LEN - 1); + if(len > 0) free(line); + return -1; + } + + if(line[i] != '\0') + { + fprintf(stderr, "%s: error: Invalid line: %s\n", argv0, line); if(len > 0) free(line); return -1; } + if(i < nread) i++; while(i < nread && line[i] == ' ') i++; diff --git a/test-cmd/sha1sum.sh b/test-cmd/sha1sum.sh @@ -42,11 +42,11 @@ t --exit=1 --input='a9993e364706816aba3e25717850c26c9cd0d89d /var/empty/e/no/en t --exit=1 --input='a9993e364706816aba3e25717850c26c9cd0d89d */var/empty/e/no/ent' 'xfail_bin_check:enoent' '-c' "sha1sum: error: Failed opening file '/var/empty/e/no/ent': No such file or directory " -t --exit=1 --input="# $WD/inputs/empty" 'invalid_chars:#' '-c' "sha1sum: error: Invalid character '#' while reading hash in line: # $WD/inputs/empty +t --exit=1 --input="#a9993e364706816aba3e25717850c26c9cd0d89d $WD/inputs/empty" 'invalid_chars:#' '-c' "sha1sum: error: Invalid character '#' while reading hash in line: #a9993e364706816aba3e25717850c26c9cd0d89d $WD/inputs/empty " -t --exit=1 --input="a9993e364706816aba3e25717850c26c9cd0d89d" 'missing_file' '-c' "sha1sum: error: Failed opening file '': No such file or directory +t --exit=1 --input="a9993e364706816aba3e25717850c26c9cd0d89d" 'missing_file' '-c' "sha1sum: error: Expected at least 44 characters, got 40 for line: a9993e364706816aba3e25717850c26c9cd0d89d " -t --exit=1 --input="a9993e364706816aba3e25717 $WD/inputs/empty" 'truncated_hash' '-c' 'sha1sum: error: Got 26 hexadecimal digits while expected 41 for a SHA1 +t --exit=1 --input="a9993e364706816aba3e25717 $WD/inputs/empty" 'truncated_hash' '-c' 'sha1sum: error: Got 25 hexadecimal digits while expected 40 for a SHA1 ' -t --exit=1 --input="a9993e364706816aba3e25717850c26c9cd0d89da9993e364706816aba3e25717850c26c9cd0d89d $WD/inputs/empty" 'elongated_hash' '-c' 'sha1sum: error: Got 81 hexadecimal digits while expected 41 for a SHA1 +t --exit=1 --input="a9993e364706816aba3e25717850c26c9cd0d89da9993e364706816aba3e25717850c26c9cd0d89d $WD/inputs/empty" 'elongated_hash' '-c' 'sha1sum: error: Got 80 hexadecimal digits while expected 40 for a SHA1 ' diff --git a/test-cmd/sha256sum.sh b/test-cmd/sha256sum.sh @@ -42,11 +42,11 @@ t --exit=1 --input='e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852 t --exit=1 --input='e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 */var/empty/e/no/ent' 'xfail_bin_check:enoent' '-c' "sha256sum: error: Failed opening file '/var/empty/e/no/ent': No such file or directory " -t --exit=1 --input="# $WD/inputs/empty" 'invalid_chars:#' '-c' "sha256sum: error: Invalid character '#' while reading hash in line: # $WD/inputs/empty +t --exit=1 --input="#ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad $WD/inputs/empty" 'invalid_chars:#' '-c' "sha256sum: error: Invalid character '#' while reading hash in line: #ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad $WD/inputs/empty " -t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" 'missing_file' '-c' "sha256sum: error: Failed opening file '': No such file or directory +t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" 'missing_file' '-c' "sha256sum: error: Expected at least 68 characters, got 64 for line: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 " -t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85 $WD/inputs/empty" 'truncated_hash' '-c' 'sha256sum: error: Got 64 hexadecimal digits while expected 65 for a SHA256 +t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85 $WD/inputs/empty" 'truncated_hash' '-c' 'sha256sum: error: Got 63 hexadecimal digits while expected 64 for a SHA256 ' -t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855d $WD/inputs/empty" 'elongated_hash' '-c' 'sha256sum: error: Got 66 hexadecimal digits while expected 65 for a SHA256 +t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855d $WD/inputs/empty" 'elongated_hash' '-c' 'sha256sum: error: Got 65 hexadecimal digits while expected 64 for a SHA256 ' diff --git a/test-cmd/sha512sum.sh b/test-cmd/sha512sum.sh @@ -42,11 +42,11 @@ t --exit=1 --input='cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36c t --exit=1 --input='cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e */var/empty/e/no/ent' 'xfail_bin_check:enoent' '-c' "sha512sum: error: Failed opening file '/var/empty/e/no/ent': No such file or directory " -t --exit=1 --input="# $WD/inputs/empty" 'invalid_chars:#' '-c' "sha512sum: error: Invalid character '#' while reading hash in line: # $WD/inputs/empty +t --exit=1 --input="#cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e $WD/inputs/empty" 'invalid_chars:#' '-c' "sha512sum: error: Invalid character '#' while reading hash in line: #cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e $WD/inputs/empty " -t --exit=1 --input="cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" 'missing_file' '-c' "sha512sum: error: Failed opening file '': No such file or directory +t --exit=1 --input="cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e" 'missing_file' '-c' "sha512sum: error: Expected at least 132 characters, got 128 for line: cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e " -t --exit=1 --input="cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3 $WD/inputs/empty" 'truncated_hash' '-c' 'sha512sum: error: Got 128 hexadecimal digits while expected 129 for a SHA512 +t --exit=1 --input="cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3 $WD/inputs/empty" 'truncated_hash' '-c' 'sha512sum: error: Got 127 hexadecimal digits while expected 128 for a SHA512 ' -t --exit=1 --input="cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3ed $WD/inputs/empty" 'elongated_hash' '-c' 'sha512sum: error: Got 130 hexadecimal digits while expected 129 for a SHA512 +t --exit=1 --input="cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3ed $WD/inputs/empty" 'elongated_hash' '-c' 'sha512sum: error: Got 129 hexadecimal digits while expected 128 for a SHA512 '