Skip to content

Commit b8505ec

Browse files
bk2204gitster
authored andcommitted
hex: default to the_hash_algo on zero algorithm value
There are numerous places in the codebase where we assume we can initialize data by zeroing all its bytes. However, when we do that with a struct object_id, it leaves the structure with a zero value for the algorithm, which is invalid. We could forbid this pattern and require that all struct object_id instances be initialized using oidclr, but this seems burdensome and it's unnatural to most C programmers. Instead, if the algorithm is zero, assume we wanted to use the default hash algorithm instead. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 71b7672 commit b8505ec

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

hex.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
124124
char *buf = buffer;
125125
int i;
126126

127+
/*
128+
* Our struct object_id has been memset to 0, so default to printing
129+
* using the default hash.
130+
*/
131+
if (algop == &hash_algos[0])
132+
algop = the_hash_algo;
133+
127134
for (i = 0; i < algop->rawsz; i++) {
128135
unsigned int val = *hash++;
129136
*buf++ = hex[val >> 4];

0 commit comments

Comments
 (0)