From 78f137e931eff3c5133fe0a58f5d469f50959556 Mon Sep 17 00:00:00 2001 From: Carlo Nucera Date: Tue, 2 Jun 2020 16:20:22 -0400 Subject: [PATCH] Validate text version instead, throw Errors --- src/libstore/content-address.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libstore/content-address.cc b/src/libstore/content-address.cc index 4c3af18fd..8a8112fd0 100644 --- a/src/libstore/content-address.cc +++ b/src/libstore/content-address.cc @@ -45,26 +45,26 @@ ContentAddress parseContentAddress(std::string_view rawCa) { auto prefix = string(rawCa, 0, prefixSeparator); if (prefix == "text") { auto hashTypeAndHash = rawCa.substr(prefixSeparator+1, string::npos); - return TextHash { Hash(string(hashTypeAndHash)) }; + Hash hash = Hash(string(hashTypeAndHash)); + if (hash.type != HashType::SHA256) { + throw Error("parseContentAddress: the text hash should have type SHA256"); + } + return TextHash { hash }; } else if (prefix == "fixed") { // This has to be an inverse to makeFixedOutputCA auto methodAndHash = rawCa.substr(prefixSeparator+1, string::npos); if (methodAndHash.substr(0,2) == "r:") { std::string_view hashRaw = methodAndHash.substr(2,string::npos); - Hash hash = Hash(string(hashRaw)); - assert(hash.type == HashType::SHA256); - return FileSystemHash { FileIngestionMethod::Recursive, hash }; + return FileSystemHash { FileIngestionMethod::Recursive, Hash(string(hashRaw)) }; } else { std::string_view hashRaw = methodAndHash; - Hash hash = Hash(string(hashRaw)); - assert(hash.type == HashType::SHA256); - return FileSystemHash { FileIngestionMethod::Flat, hash }; + return FileSystemHash { FileIngestionMethod::Flat, Hash(string(hashRaw)) }; } } else { - throw "parseContentAddress: format not recognized; has to be text or fixed"; + throw Error("parseContentAddress: format not recognized; has to be text or fixed"); } } else { - throw "Not a content address because it lacks an appropriate prefix"; + throw Error("Not a content address because it lacks an appropriate prefix"); } };