lib/util_hash.c: add conditions to avoid misuse without asserts

This commit is contained in:
Duncan Overbruck 2020-02-10 14:09:42 +01:00 committed by Juan RP
parent d99aae586a
commit fbba38b006

View File

@ -116,7 +116,11 @@ xbps_file_sha256_raw(unsigned char *dst, size_t dstlen, const char *file)
char buf[65536]; char buf[65536];
SHA256_CTX sha256; SHA256_CTX sha256;
assert(dstlen >= SHA256_DIGEST_LENGTH); assert(dstlen >= XBPS_SHA256_DIGEST_SIZE);
if (dstlen < XBPS_SHA256_DIGEST_SIZE) {
errno = ENOBUFS;
return false;
}
if ((fd = open(file, O_RDONLY)) < 0) if ((fd = open(file, O_RDONLY)) < 0)
return false; return false;
@ -142,6 +146,10 @@ xbps_file_sha256(char *dst, size_t dstlen, const char *file)
unsigned char digest[XBPS_SHA256_DIGEST_SIZE]; unsigned char digest[XBPS_SHA256_DIGEST_SIZE];
assert(dstlen >= XBPS_SHA256_SIZE); assert(dstlen >= XBPS_SHA256_SIZE);
if (dstlen < XBPS_SHA256_SIZE) {
errno = ENOBUFS;
return false;
}
if (!xbps_file_sha256_raw(digest, sizeof digest, file)) if (!xbps_file_sha256_raw(digest, sizeof digest, file))
return false; return false;
@ -155,12 +163,15 @@ static bool
sha256_digest_compare(const char *sha256, size_t shalen, sha256_digest_compare(const char *sha256, size_t shalen,
const unsigned char *digest, size_t digestlen) const unsigned char *digest, size_t digestlen)
{ {
assert(digestlen == XBPS_SHA256_DIGEST_SIZE);
assert(shalen == XBPS_SHA256_SIZE - 1);
assert(shalen == XBPS_SHA256_SIZE - 1);
if (shalen != XBPS_SHA256_SIZE -1) if (shalen != XBPS_SHA256_SIZE -1)
return false; return false;
assert(digestlen == XBPS_SHA256_DIGEST_SIZE);
if (digestlen != XBPS_SHA256_DIGEST_SIZE)
return false;
for (; *sha256;) { for (; *sha256;) {
if (*digest / 16 < 10) { if (*digest / 16 < 10) {
if (*sha256++ != '0' + *digest / 16) if (*sha256++ != '0' + *digest / 16)