From 407175a1d0e29f45639e7f28a531b3981cd20085 Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Mon, 9 Jan 2023 16:48:01 +0100 Subject: daemon: Improve error message for wrong hash sizes. * nix/libutil/hash.cc (parseHash): Show the hash algorithm name and expected size in the error message. * tests/derivations.scm ("fixed-output derivation, invalid hash size"): New test. --- nix/libutil/hash.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'nix/libutil') diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc index 9ba604eb85..9b83ffcdd9 100644 --- a/nix/libutil/hash.cc +++ b/nix/libutil/hash.cc @@ -76,8 +76,11 @@ string printHash(const Hash & hash) Hash parseHash(HashType ht, const string & s) { Hash hash(ht); - if (s.length() != hash.hashSize * 2) - throw Error(format("invalid hash `%1%'") % s); + if (s.length() != hash.hashSize * 2) { + string algo = gcry_md_algo_name(ht); + throw Error(format("invalid %1% hash '%2%' (%3% bytes but expected %4%)") + % algo % s % (s.length() / 2) % hash.hashSize); + } for (unsigned int i = 0; i < hash.hashSize; i++) { string s2(s, i * 2, 2); if (!isxdigit(s2[0]) || !isxdigit(s2[1])) -- cgit v1.2.3