fix(3p/nix/hash): param of Unknown allows any hash type

Fixes a crash in the self-hosting instantiate test:

NIX_REMOTE="$(mktemp -d)" nix-instantiate -E 'let depot = import ./default.nix {}; in depot.third_party.nix.outPath'

Change-Id: If99494aa07ec248d3894d4709ab0fde7fa81aff3
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1508
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Kane York 2020-07-31 17:37:15 -07:00 committed by kanepyork
parent 1cbffe21f3
commit 674dbade27

View file

@ -6,6 +6,7 @@
#include <absl/strings/escaping.h>
#include <absl/strings/str_format.h>
#include <fcntl.h>
#include <glog/logging.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <sys/stat.h>
@ -182,6 +183,7 @@ Hash::Hash(std::string_view s, HashType type) : type(type) {
*this = unwrap_throw(result);
}
// TODO(riking): change ht to an optional
absl::StatusOr<Hash> Hash::deserialize(std::string_view s, HashType type) {
size_t pos = 0;
bool isSRI = false;
@ -201,7 +203,7 @@ absl::StatusOr<Hash> Hash::deserialize(std::string_view s, HashType type) {
if (sep != std::string::npos) {
std::string hts = std::string(s, 0, sep);
parsedType = parseHashType(hts);
if (parsedType != type) {
if (type != htUnknown && parsedType != type) {
return absl::InvalidArgumentError(
absl::StrCat("hash '", s, "' should have type '", printHashType(type),
"', found '", printHashType(parsedType), "'"));
@ -432,7 +434,10 @@ std::string printHashType(HashType ht) {
return "sha256";
} else if (ht == htSHA512) {
return "sha512";
} else if (ht == htUnknown) {
return "<unknown>";
} else {
LOG(FATAL) << "Unrecognized hash type: " << static_cast<int>(ht);
abort();
}
}