Skip to content

Commit

Permalink
hex: default to the_hash_algo on zero algorithm value
Browse files Browse the repository at this point in the history
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 <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
bk2204 authored and gitster committed Apr 27, 2021
1 parent 71b7672 commit b8505ec
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ char *hash_to_hex_algop_r(char *buffer, const unsigned char *hash,
char *buf = buffer;
int i;

/*
* Our struct object_id has been memset to 0, so default to printing
* using the default hash.
*/
if (algop == &hash_algos[0])
algop = the_hash_algo;

for (i = 0; i < algop->rawsz; i++) {
unsigned int val = *hash++;
*buf++ = hex[val >> 4];
Expand Down

0 comments on commit b8505ec

Please sign in to comment.