diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-09-20 16:15:32 +0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-09-20 16:15:32 +0000 |
commit | 0623359fbc67c421bf76b7433f92c7ef58050321 (patch) | |
tree | b0621f97bbbea2ae2ad69a89284454aa1f0c85c0 | |
parent | 22d13d6ec27b02f98ca4e398fbae4f3273e97794 (diff) | |
download | guix-0623359fbc67c421bf76b7433f92c7ef58050321.tar.gz |
* Print a better error message for wrong hashes (NIX-49).
-rw-r--r-- | src/libexpr/primops.cc | 9 | ||||
-rw-r--r-- | src/libutil/hash.cc | 8 | ||||
-rw-r--r-- | src/libutil/hash.hh | 3 |
3 files changed, 16 insertions, 4 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 3f915fc232..c04d41bf4d 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -353,13 +353,16 @@ static Expr primDerivationStrict(EvalState & state, const ATermVector & args) HashType ht = parseHashType(outputHashAlgo); if (ht == htUnknown) throw EvalError(format("unknown hash algorithm `%1%'") % outputHashAlgo); - Hash h; - if (outputHash.size() == Hash(ht).hashSize * 2) + Hash h(ht); + if (outputHash.size() == h.hashSize * 2) /* hexadecimal representation */ h = parseHash(ht, outputHash); - else + else if (outputHash.size() == hashLength32(h)) /* base-32 representation */ h = parseHash32(ht, outputHash); + else + throw Error(format("hash `%1%' has wrong length for hash type `%2%'") + % outputHash % outputHashAlgo); string s = outputHash; outputHash = printHash(h); if (outputHashRecursive) outputHashAlgo = "r:" + outputHashAlgo; diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 16597fd47c..7381948f21 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -120,6 +120,12 @@ static unsigned char divMod(unsigned char * bytes, unsigned char y) } +unsigned int hashLength32(const Hash & hash) +{ + return (hash.hashSize * 8 - 1) / 5 + 1; +} + + // omitted: E O U T const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz"; @@ -127,7 +133,7 @@ const string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz"; string printHash32(const Hash & hash) { Hash hash2(hash); - unsigned int len = (hash.hashSize * 8 - 1) / 5 + 1; + unsigned int len = hashLength32(hash); const char * chars = base32Chars.c_str(); diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index 95629bc9e5..74ae51db32 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -49,6 +49,9 @@ string printHash(const Hash & hash); /* Parse a hexadecimal representation of a hash code. */ Hash parseHash(HashType ht, const string & s); +/* Returns the length of a base-32 hash representation. */ +unsigned int hashLength32(const Hash & hash); + /* Convert a hash to a base-32 representation. */ string printHash32(const Hash & hash); |