Commit 03dc6480b41bdfa33911784d772d612f8e5c023b

Patrick Steinhardt 2019-01-02T09:27:44

hash: convert `global_init` macros to real function The `git_hash_global_init` function is simply defined as a macro to zero for most of the different hash implementations. This makes it impossible to treat it like a function pointer, which is required for a later commit where we want to improve the way global initialization works. Fix the issue by converting all no-op macros to an inline function returning zero. There's a small gotcha here, though: as most hash implementations only have a header file, but not a corresponding implementation file, we cannot declare the function as non-static. But declaring it as `static inline` fails, too, as there is a previous declaration as non-static. So we have to move the function declaration after the include that brings in the function definition, as it is allowed to have a non-static declaration after a static definition, but not the other way round.

diff --git a/src/hash.h b/src/hash.h
index 93765ad..0502e35 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -14,7 +14,6 @@
 typedef struct git_hash_prov git_hash_prov;
 typedef struct git_hash_ctx git_hash_ctx;
 
-int git_hash_global_init(void);
 int git_hash_ctx_init(git_hash_ctx *ctx);
 void git_hash_ctx_cleanup(git_hash_ctx *ctx);
 
@@ -32,6 +31,8 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx);
 # include "hash/hash_generic.h"
 #endif
 
+int git_hash_global_init(void);
+
 typedef struct {
 	void *data;
 	size_t len;
diff --git a/src/hash/hash_collisiondetect.h b/src/hash/hash_collisiondetect.h
index 4c5e3c3..4e4c7da 100644
--- a/src/hash/hash_collisiondetect.h
+++ b/src/hash/hash_collisiondetect.h
@@ -15,10 +15,14 @@ struct git_hash_ctx {
 	SHA1_CTX c;
 };
 
-#define git_hash_global_init() 0
 #define git_hash_ctx_init(ctx) git_hash_init(ctx)
 #define git_hash_ctx_cleanup(ctx)
 
+GIT_INLINE(int) git_hash_global_init(void)
+{
+	return 0;
+}
+
 GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
 {
 	assert(ctx);
diff --git a/src/hash/hash_common_crypto.h b/src/hash/hash_common_crypto.h
index 5c3887d..ce352a6 100644
--- a/src/hash/hash_common_crypto.h
+++ b/src/hash/hash_common_crypto.h
@@ -18,10 +18,14 @@ struct git_hash_ctx {
 
 #define CC_LONG_MAX ((CC_LONG)-1)
 
-#define git_hash_global_init() 0
 #define git_hash_ctx_init(ctx) git_hash_init(ctx)
 #define git_hash_ctx_cleanup(ctx)
 
+GIT_INLINE(int) git_hash_global_init(void)
+{
+	return 0;
+}
+
 GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
 {
 	assert(ctx);
diff --git a/src/hash/hash_generic.h b/src/hash/hash_generic.h
index 21a0428..fb0009c 100644
--- a/src/hash/hash_generic.h
+++ b/src/hash/hash_generic.h
@@ -18,8 +18,12 @@ struct git_hash_ctx {
 	unsigned int W[16];
 };
 
-#define git_hash_global_init() 0
 #define git_hash_ctx_init(ctx) git_hash_init(ctx)
 #define git_hash_ctx_cleanup(ctx)
 
+GIT_INLINE(int) git_hash_global_init(void)
+{
+	return 0;
+}
+
 #endif
diff --git a/src/hash/hash_mbedtls.h b/src/hash/hash_mbedtls.h
index 24196c5..7f3decd 100644
--- a/src/hash/hash_mbedtls.h
+++ b/src/hash/hash_mbedtls.h
@@ -14,7 +14,11 @@ struct git_hash_ctx {
     mbedtls_sha1_context c;
 };
 
-#define git_hash_global_init() 0
 #define git_hash_ctx_init(ctx) git_hash_init(ctx)
 
+GIT_INLINE(int) git_hash_global_init(void)
+{
+	return 0;
+}
+
 #endif /* INCLUDE_hash_mbedtld_h__ */
diff --git a/src/hash/hash_openssl.h b/src/hash/hash_openssl.h
index eb2dcb0..9a32cba 100644
--- a/src/hash/hash_openssl.h
+++ b/src/hash/hash_openssl.h
@@ -16,10 +16,14 @@ struct git_hash_ctx {
 	SHA_CTX c;
 };
 
-#define git_hash_global_init() 0
 #define git_hash_ctx_init(ctx) git_hash_init(ctx)
 #define git_hash_ctx_cleanup(ctx)
 
+GIT_INLINE(int) git_hash_global_init(void)
+{
+	return 0;
+}
+
 GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
 {
 	assert(ctx);