Skip to content

Commit 03dc648

Browse files
committed
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.
1 parent 0ddc609 commit 03dc648

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

src/hash.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
typedef struct git_hash_prov git_hash_prov;
1515
typedef struct git_hash_ctx git_hash_ctx;
1616

17-
int git_hash_global_init(void);
1817
int git_hash_ctx_init(git_hash_ctx *ctx);
1918
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
2019

@@ -32,6 +31,8 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx);
3231
# include "hash/hash_generic.h"
3332
#endif
3433

34+
int git_hash_global_init(void);
35+
3536
typedef struct {
3637
void *data;
3738
size_t len;

src/hash/hash_collisiondetect.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ struct git_hash_ctx {
1515
SHA1_CTX c;
1616
};
1717

18-
#define git_hash_global_init() 0
1918
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2019
#define git_hash_ctx_cleanup(ctx)
2120

21+
GIT_INLINE(int) git_hash_global_init(void)
22+
{
23+
return 0;
24+
}
25+
2226
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
2327
{
2428
assert(ctx);

src/hash/hash_common_crypto.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ struct git_hash_ctx {
1818

1919
#define CC_LONG_MAX ((CC_LONG)-1)
2020

21-
#define git_hash_global_init() 0
2221
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2322
#define git_hash_ctx_cleanup(ctx)
2423

24+
GIT_INLINE(int) git_hash_global_init(void)
25+
{
26+
return 0;
27+
}
28+
2529
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
2630
{
2731
assert(ctx);

src/hash/hash_generic.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ struct git_hash_ctx {
1818
unsigned int W[16];
1919
};
2020

21-
#define git_hash_global_init() 0
2221
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2322
#define git_hash_ctx_cleanup(ctx)
2423

24+
GIT_INLINE(int) git_hash_global_init(void)
25+
{
26+
return 0;
27+
}
28+
2529
#endif

src/hash/hash_mbedtls.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ struct git_hash_ctx {
1414
mbedtls_sha1_context c;
1515
};
1616

17-
#define git_hash_global_init() 0
1817
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
1918

19+
GIT_INLINE(int) git_hash_global_init(void)
20+
{
21+
return 0;
22+
}
23+
2024
#endif /* INCLUDE_hash_mbedtld_h__ */

src/hash/hash_openssl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ struct git_hash_ctx {
1616
SHA_CTX c;
1717
};
1818

19-
#define git_hash_global_init() 0
2019
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2120
#define git_hash_ctx_cleanup(ctx)
2221

22+
GIT_INLINE(int) git_hash_global_init(void)
23+
{
24+
return 0;
25+
}
26+
2327
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
2428
{
2529
assert(ctx);

0 commit comments

Comments
 (0)