Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Report errors in sha256_file()
jlaffaye committed 15 years ago
commit 43e9019e8fbdd3bcfcb3fa4d0418fe1576797b95
parent 30db1d3
1 file changed +15 -8
modified libpkg/pkg_util.c
@@ -257,25 +257,32 @@ sha256_str(const char *string, char out[65])
int
sha256_file(const char *path, char out[65])
{
-
	FILE *file = fopen(path, "rb");
+
	FILE *fp;
	char buffer[BUFSIZ];
	unsigned char hash[SHA256_DIGEST_LENGTH];
-
	int r = 0;
+
	size_t r = 0;
	SHA256_CTX sha256;

-
	if (!file) return -1;
+
	if ((fp = fopen(path, "rb")) == NULL)
+
		return (pkg_error_set(EPKG_FATAL, "fopen(%s): %s", path,
+
							  strerror(errno)));

	SHA256_Init(&sha256);

-
	while ((r = fread(buffer, 1, BUFSIZ, file)))
+
	while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
		SHA256_Update(&sha256, buffer, r);

-
	SHA256_Final(hash, &sha256);
+
	fclose(fp);

-
	sha256_hash(hash, out);
+
	if (ferror(fp) != 0) {
+
		out[0] = '\0';
+
		return (pkg_error_set(EPKG_FATAL, "fread(%s): %s", path,
+
							  strerror(errno)));

-
	fclose(file);
+
	}

-
	return 0;
+
	SHA256_Final(hash, &sha256);
+
	sha256_hash(hash, out);
+
	return (EPKG_OK);
}