Commit 3ff56dae29a213110255a30f5085ad290c98d717

Edward Thomson 2020-11-21T23:35:09

hash: use GIT_ASSERT

diff --git a/src/hash/sha1/collisiondetect.c b/src/hash/sha1/collisiondetect.c
index e6a1267..722ebf3 100644
--- a/src/hash/sha1/collisiondetect.c
+++ b/src/hash/sha1/collisiondetect.c
@@ -24,21 +24,21 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
 
 int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
 {
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 	SHA1DCInit(&ctx->c);
 	return 0;
 }
 
 int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
 {
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 	SHA1DCUpdate(&ctx->c, data, len);
 	return 0;
 }
 
 int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
 {
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 	if (SHA1DCFinal(out->id, &ctx->c)) {
 		git_error_set(GIT_ERROR_SHA1, "SHA1 collision attack detected");
 		return -1;
diff --git a/src/hash/sha1/common_crypto.c b/src/hash/sha1/common_crypto.c
index 0449a3c..4250e0b 100644
--- a/src/hash/sha1/common_crypto.c
+++ b/src/hash/sha1/common_crypto.c
@@ -26,7 +26,7 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
 
 int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
 {
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 	CC_SHA1_Init(&ctx->c);
 	return 0;
 }
@@ -35,7 +35,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
 {
 	const unsigned char *data = _data;
 
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 
 	while (len > 0) {
 		CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len;
@@ -51,7 +51,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
 
 int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
 {
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 	CC_SHA1_Final(out->id, &ctx->c);
 	return 0;
 }
diff --git a/src/hash/sha1/mbedtls.c b/src/hash/sha1/mbedtls.c
index e44343f..04e7da5 100644
--- a/src/hash/sha1/mbedtls.c
+++ b/src/hash/sha1/mbedtls.c
@@ -19,28 +19,28 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
 
 void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
 {
-    assert(ctx);
-    mbedtls_sha1_free(&ctx->c);
+	if (ctx)
+		mbedtls_sha1_free(&ctx->c);
 }
 
 int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
 {
-    assert(ctx);
-    mbedtls_sha1_init(&ctx->c);
-    mbedtls_sha1_starts(&ctx->c);
-    return 0;
+	GIT_ASSERT_ARG(ctx);
+	mbedtls_sha1_init(&ctx->c);
+	mbedtls_sha1_starts(&ctx->c);
+	return 0;
 }
 
 int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
 {
-    assert(ctx);
-    mbedtls_sha1_update(&ctx->c, data, len);
-    return 0;
+	GIT_ASSERT_ARG(ctx);
+	mbedtls_sha1_update(&ctx->c, data, len);
+	return 0;
 }
 
 int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
 {
-    assert(ctx);
-    mbedtls_sha1_finish(&ctx->c, out->id);
-    return 0;
+	GIT_ASSERT_ARG(ctx);
+	mbedtls_sha1_finish(&ctx->c, out->id);
+	return 0;
 }
diff --git a/src/hash/sha1/openssl.c b/src/hash/sha1/openssl.c
index ba3212f..68d9611 100644
--- a/src/hash/sha1/openssl.c
+++ b/src/hash/sha1/openssl.c
@@ -24,7 +24,7 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
 
 int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
 {
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 
 	if (SHA1_Init(&ctx->c) != 1) {
 		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to initialize hash context");
@@ -36,7 +36,7 @@ int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
 
 int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
 {
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 
 	if (SHA1_Update(&ctx->c, data, len) != 1) {
 		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to update hash");
@@ -48,7 +48,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
 
 int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
 {
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 
 	if (SHA1_Final(out->id, &ctx->c) != 1) {
 		git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash");
diff --git a/src/hash/sha1/win32.c b/src/hash/sha1/win32.c
index b1266cc..a6e7061 100644
--- a/src/hash/sha1/win32.c
+++ b/src/hash/sha1/win32.c
@@ -164,7 +164,7 @@ GIT_INLINE(int) hash_cryptoapi_update(git_hash_sha1_ctx *ctx, const void *_data,
 {
 	const BYTE *data = (BYTE *)_data;
 
-	assert(ctx->ctx.cryptoapi.valid);
+	GIT_ASSERT(ctx->ctx.cryptoapi.valid);
 
 	while (len > 0) {
 		DWORD chunk = (len > MAXDWORD) ? MAXDWORD : (DWORD)len;
@@ -186,7 +186,7 @@ GIT_INLINE(int) hash_cryptoapi_final(git_oid *out, git_hash_sha1_ctx *ctx)
 	DWORD len = 20;
 	int error = 0;
 
-	assert(ctx->ctx.cryptoapi.valid);
+	GIT_ASSERT(ctx->ctx.cryptoapi.valid);
 
 	if (!CryptGetHashParam(ctx->ctx.cryptoapi.hash_handle, HP_HASHVAL, out->id, &len, 0)) {
 		git_error_set(GIT_ERROR_OS, "legacy hash data could not be finished");
@@ -286,7 +286,7 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
 {
 	int error = 0;
 
-	assert(ctx);
+	GIT_ASSERT_ARG(ctx);
 
 	/*
 	 * When compiled with GIT_THREADS, the global hash_prov data is
@@ -303,27 +303,30 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
 
 int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
 {
-	assert(ctx && ctx->type);
+	GIT_ASSERT_ARG(ctx);
+	GIT_ASSERT_ARG(ctx->type);
 	return (ctx->type == CNG) ? hash_cng_init(ctx) : hash_cryptoapi_init(ctx);
 }
 
 int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
 {
-	assert(ctx && ctx->type);
+	GIT_ASSERT_ARG(ctx);
+	GIT_ASSERT_ARG(ctx->type);
 	return (ctx->type == CNG) ? hash_cng_update(ctx, data, len) : hash_cryptoapi_update(ctx, data, len);
 }
 
 int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
 {
-	assert(ctx && ctx->type);
+	GIT_ASSERT_ARG(ctx);
+	GIT_ASSERT_ARG(ctx->type);
 	return (ctx->type == CNG) ? hash_cng_final(out, ctx) : hash_cryptoapi_final(out, ctx);
 }
 
 void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
 {
-	assert(ctx);
-
-	if (ctx->type == CNG)
+	if (!ctx)
+		return;
+	else if (ctx->type == CNG)
 		hash_ctx_cng_cleanup(ctx);
 	else if(ctx->type == CRYPTOAPI)
 		hash_ctx_cryptoapi_cleanup(ctx);