Skip to content

Commit 3ff56da

Browse files
committed
hash: use GIT_ASSERT
1 parent cac3600 commit 3ff56da

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed

src/hash/sha1/collisiondetect.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
2424

2525
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2626
{
27-
assert(ctx);
27+
GIT_ASSERT_ARG(ctx);
2828
SHA1DCInit(&ctx->c);
2929
return 0;
3030
}
3131

3232
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
3333
{
34-
assert(ctx);
34+
GIT_ASSERT_ARG(ctx);
3535
SHA1DCUpdate(&ctx->c, data, len);
3636
return 0;
3737
}
3838

3939
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
4040
{
41-
assert(ctx);
41+
GIT_ASSERT_ARG(ctx);
4242
if (SHA1DCFinal(out->id, &ctx->c)) {
4343
git_error_set(GIT_ERROR_SHA1, "SHA1 collision attack detected");
4444
return -1;

src/hash/sha1/common_crypto.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
2626

2727
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2828
{
29-
assert(ctx);
29+
GIT_ASSERT_ARG(ctx);
3030
CC_SHA1_Init(&ctx->c);
3131
return 0;
3232
}
@@ -35,7 +35,7 @@ int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *_data, size_t len)
3535
{
3636
const unsigned char *data = _data;
3737

38-
assert(ctx);
38+
GIT_ASSERT_ARG(ctx);
3939

4040
while (len > 0) {
4141
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)
5151

5252
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
5353
{
54-
assert(ctx);
54+
GIT_ASSERT_ARG(ctx);
5555
CC_SHA1_Final(out->id, &ctx->c);
5656
return 0;
5757
}

src/hash/sha1/mbedtls.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
1919

2020
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
2121
{
22-
assert(ctx);
23-
mbedtls_sha1_free(&ctx->c);
22+
if (ctx)
23+
mbedtls_sha1_free(&ctx->c);
2424
}
2525

2626
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2727
{
28-
assert(ctx);
29-
mbedtls_sha1_init(&ctx->c);
30-
mbedtls_sha1_starts(&ctx->c);
31-
return 0;
28+
GIT_ASSERT_ARG(ctx);
29+
mbedtls_sha1_init(&ctx->c);
30+
mbedtls_sha1_starts(&ctx->c);
31+
return 0;
3232
}
3333

3434
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
3535
{
36-
assert(ctx);
37-
mbedtls_sha1_update(&ctx->c, data, len);
38-
return 0;
36+
GIT_ASSERT_ARG(ctx);
37+
mbedtls_sha1_update(&ctx->c, data, len);
38+
return 0;
3939
}
4040

4141
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
4242
{
43-
assert(ctx);
44-
mbedtls_sha1_finish(&ctx->c, out->id);
45-
return 0;
43+
GIT_ASSERT_ARG(ctx);
44+
mbedtls_sha1_finish(&ctx->c, out->id);
45+
return 0;
4646
}

src/hash/sha1/openssl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
2424

2525
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
2626
{
27-
assert(ctx);
27+
GIT_ASSERT_ARG(ctx);
2828

2929
if (SHA1_Init(&ctx->c) != 1) {
3030
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)
3636

3737
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
3838
{
39-
assert(ctx);
39+
GIT_ASSERT_ARG(ctx);
4040

4141
if (SHA1_Update(&ctx->c, data, len) != 1) {
4242
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)
4848

4949
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
5050
{
51-
assert(ctx);
51+
GIT_ASSERT_ARG(ctx);
5252

5353
if (SHA1_Final(out->id, &ctx->c) != 1) {
5454
git_error_set(GIT_ERROR_SHA1, "hash_openssl: failed to finalize hash");

src/hash/sha1/win32.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ GIT_INLINE(int) hash_cryptoapi_update(git_hash_sha1_ctx *ctx, const void *_data,
164164
{
165165
const BYTE *data = (BYTE *)_data;
166166

167-
assert(ctx->ctx.cryptoapi.valid);
167+
GIT_ASSERT(ctx->ctx.cryptoapi.valid);
168168

169169
while (len > 0) {
170170
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)
186186
DWORD len = 20;
187187
int error = 0;
188188

189-
assert(ctx->ctx.cryptoapi.valid);
189+
GIT_ASSERT(ctx->ctx.cryptoapi.valid);
190190

191191
if (!CryptGetHashParam(ctx->ctx.cryptoapi.hash_handle, HP_HASHVAL, out->id, &len, 0)) {
192192
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)
286286
{
287287
int error = 0;
288288

289-
assert(ctx);
289+
GIT_ASSERT_ARG(ctx);
290290

291291
/*
292292
* 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)
303303

304304
int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
305305
{
306-
assert(ctx && ctx->type);
306+
GIT_ASSERT_ARG(ctx);
307+
GIT_ASSERT_ARG(ctx->type);
307308
return (ctx->type == CNG) ? hash_cng_init(ctx) : hash_cryptoapi_init(ctx);
308309
}
309310

310311
int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
311312
{
312-
assert(ctx && ctx->type);
313+
GIT_ASSERT_ARG(ctx);
314+
GIT_ASSERT_ARG(ctx->type);
313315
return (ctx->type == CNG) ? hash_cng_update(ctx, data, len) : hash_cryptoapi_update(ctx, data, len);
314316
}
315317

316318
int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
317319
{
318-
assert(ctx && ctx->type);
320+
GIT_ASSERT_ARG(ctx);
321+
GIT_ASSERT_ARG(ctx->type);
319322
return (ctx->type == CNG) ? hash_cng_final(out, ctx) : hash_cryptoapi_final(out, ctx);
320323
}
321324

322325
void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
323326
{
324-
assert(ctx);
325-
326-
if (ctx->type == CNG)
327+
if (!ctx)
328+
return;
329+
else if (ctx->type == CNG)
327330
hash_ctx_cng_cleanup(ctx);
328331
else if(ctx->type == CRYPTOAPI)
329332
hash_ctx_cryptoapi_cleanup(ctx);

0 commit comments

Comments
 (0)