Skip to content

Commit f483657

Browse files
pks-tgitster
authored andcommitted
hash: require hash algorithm in hasheq(), hashcmp() and hashclr()
Many of our hash functions have two variants, one receiving a `struct git_hash_algo` and one that derives it via `the_repository`. Adapt all of those functions to always require the hash algorithm as input and drop the variants that do not accept one. As those functions are now independent of `the_repository`, we can move them from "hash.h" to "hash-ll.h". Note that both in this and subsequent commits in this series we always just pass `the_repository->hash_algo` as input even if it is obvious that there is a repository in the context that we should be using the hash from instead. This is done to be on the safe side and not introduce any regressions. All callsites should eventually be amended to use a repo passed via parameters, but this is outside the scope of this patch series. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 129cb1b commit f483657

17 files changed

+52
-53
lines changed

builtin/index-pack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ static void parse_pack_objects(unsigned char *hash)
12041204
the_hash_algo->init_fn(&tmp_ctx);
12051205
the_hash_algo->clone_fn(&tmp_ctx, &input_ctx);
12061206
the_hash_algo->final_fn(hash, &tmp_ctx);
1207-
if (!hasheq(fill(the_hash_algo->rawsz), hash))
1207+
if (!hasheq(fill(the_hash_algo->rawsz), hash, the_repository->hash_algo))
12081208
die(_("pack is corrupted (SHA1 mismatch)"));
12091209
use(the_hash_algo->rawsz);
12101210

@@ -1307,11 +1307,11 @@ static void conclude_pack(int fix_thin_pack, const char *curr_pack, unsigned cha
13071307
stop_progress_msg(&progress, msg.buf);
13081308
strbuf_release(&msg);
13091309
finalize_hashfile(f, tail_hash, FSYNC_COMPONENT_PACK, 0);
1310-
hashcpy(read_hash, pack_hash);
1310+
hashcpy(read_hash, pack_hash, the_repository->hash_algo);
13111311
fixup_pack_header_footer(output_fd, pack_hash,
13121312
curr_pack, nr_objects,
13131313
read_hash, consumed_bytes-the_hash_algo->rawsz);
1314-
if (!hasheq(read_hash, tail_hash))
1314+
if (!hasheq(read_hash, tail_hash, the_repository->hash_algo))
13151315
die(_("Unexpected tail checksum for %s "
13161316
"(disk corruption?)"), curr_pack);
13171317
}

builtin/pack-redundant.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static inline struct llist_item * llist_sorted_remove(struct llist *list, const
155155
l = (hint == NULL) ? list->front : hint;
156156
prev = NULL;
157157
while (l) {
158-
const int cmp = hashcmp(l->oid.hash, oid);
158+
const int cmp = hashcmp(l->oid.hash, oid, the_repository->hash_algo);
159159
if (cmp > 0) /* not in list, since sorted */
160160
return prev;
161161
if (!cmp) { /* found */
@@ -258,7 +258,8 @@ static void cmp_two_packs(struct pack_list *p1, struct pack_list *p2)
258258
while (p1_off < p1->pack->num_objects * p1_step &&
259259
p2_off < p2->pack->num_objects * p2_step)
260260
{
261-
const int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
261+
const int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off,
262+
the_repository->hash_algo);
262263
/* cmp ~ p1 - p2 */
263264
if (cmp == 0) {
264265
p1_hint = llist_sorted_remove(p1->unique_objects,
@@ -296,7 +297,8 @@ static size_t sizeof_union(struct packed_git *p1, struct packed_git *p2)
296297
while (p1_off < p1->num_objects * p1_step &&
297298
p2_off < p2->num_objects * p2_step)
298299
{
299-
int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off);
300+
int cmp = hashcmp(p1_base + p1_off, p2_base + p2_off,
301+
the_repository->hash_algo);
300302
/* cmp ~ p1 - p2 */
301303
if (cmp == 0) {
302304
ret++;

builtin/unpack-objects.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,8 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix UNUSED)
674674
if (fsck_finish(&fsck_options))
675675
die(_("fsck error in pack objects"));
676676
}
677-
if (!hasheq(fill(the_hash_algo->rawsz), oid.hash))
677+
if (!hasheq(fill(the_hash_algo->rawsz), oid.hash,
678+
the_repository->hash_algo))
678679
die("final sha1 did not match");
679680
use(the_hash_algo->rawsz);
680681

commit-graph.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ static int add_graph_to_chain(struct commit_graph *g,
565565

566566
if (!cur_g ||
567567
!oideq(&oids[n], &cur_g->oid) ||
568-
!hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
568+
!hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n),
569+
the_repository->hash_algo)) {
569570
warning(_("commit-graph chain does not match"));
570571
return 0;
571572
}

csum-file.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
6868
hashflush(f);
6969

7070
if (f->skip_hash)
71-
hashclr(f->buffer);
71+
hashclr(f->buffer, the_repository->hash_algo);
7272
else
7373
the_hash_algo->final_fn(f->buffer, &f->ctx);
7474

7575
if (result)
76-
hashcpy(result, f->buffer);
76+
hashcpy(result, f->buffer, the_repository->hash_algo);
7777
if (flags & CSUM_HASH_IN_STREAM)
7878
flush(f, f->buffer, the_hash_algo->rawsz);
7979
if (flags & CSUM_FSYNC)
@@ -237,5 +237,5 @@ int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
237237
the_hash_algo->update_fn(&ctx, data, data_len);
238238
the_hash_algo->final_fn(got, &ctx);
239239

240-
return hasheq(got, data + data_len);
240+
return hasheq(got, data + data_len, the_repository->hash_algo);
241241
}

hash-ll.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
245245

246246
const struct object_id *null_oid(void);
247247

248-
static inline int hashcmp_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
248+
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
249249
{
250250
/*
251251
* Teach the compiler that there are only two possibilities of hash size
@@ -256,7 +256,7 @@ static inline int hashcmp_algop(const unsigned char *sha1, const unsigned char *
256256
return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
257257
}
258258

259-
static inline int hasheq_algop(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
259+
static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2, const struct git_hash_algo *algop)
260260
{
261261
/*
262262
* We write this here instead of deferring to hashcmp so that the
@@ -267,6 +267,17 @@ static inline int hasheq_algop(const unsigned char *sha1, const unsigned char *s
267267
return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
268268
}
269269

270+
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src,
271+
const struct git_hash_algo *algop)
272+
{
273+
memcpy(sha_dst, sha_src, algop->rawsz);
274+
}
275+
276+
static inline void hashclr(unsigned char *hash, const struct git_hash_algo *algop)
277+
{
278+
memset(hash, 0, algop->rawsz);
279+
}
280+
270281
static inline void oidcpy(struct object_id *dst, const struct object_id *src)
271282
{
272283
memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);

hash-lookup.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ int bsearch_hash(const unsigned char *hash, const uint32_t *fanout_nbo,
112112

113113
while (lo < hi) {
114114
unsigned mi = lo + (hi - lo) / 2;
115-
int cmp = hashcmp(table + mi * stride, hash);
115+
int cmp = hashcmp(table + mi * stride, hash,
116+
the_repository->hash_algo);
116117

117118
if (!cmp) {
118119
if (result)

hash.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,14 @@
66

77
#define the_hash_algo the_repository->hash_algo
88

9-
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
10-
{
11-
return hashcmp_algop(sha1, sha2, the_hash_algo);
12-
}
13-
149
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
1510
{
1611
const struct git_hash_algo *algop;
1712
if (!oid1->algo)
1813
algop = the_hash_algo;
1914
else
2015
algop = &hash_algos[oid1->algo];
21-
return hashcmp_algop(oid1->hash, oid2->hash, algop);
22-
}
23-
24-
static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
25-
{
26-
return hasheq_algop(sha1, sha2, the_hash_algo);
16+
return hashcmp(oid1->hash, oid2->hash, algop);
2717
}
2818

2919
static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
@@ -33,19 +23,14 @@ static inline int oideq(const struct object_id *oid1, const struct object_id *oi
3323
algop = the_hash_algo;
3424
else
3525
algop = &hash_algos[oid1->algo];
36-
return hasheq_algop(oid1->hash, oid2->hash, algop);
26+
return hasheq(oid1->hash, oid2->hash, algop);
3727
}
3828

3929
static inline int is_null_oid(const struct object_id *oid)
4030
{
4131
return oideq(oid, null_oid());
4232
}
4333

44-
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
45-
{
46-
memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
47-
}
48-
4934
/* Like oidcpy() but zero-pads the unused bytes in dst's hash array. */
5035
static inline void oidcpy_with_padding(struct object_id *dst,
5136
const struct object_id *src)
@@ -62,11 +47,6 @@ static inline void oidcpy_with_padding(struct object_id *dst,
6247
dst->algo = src->algo;
6348
}
6449

65-
static inline void hashclr(unsigned char *hash)
66-
{
67-
memset(hash, 0, the_hash_algo->rawsz);
68-
}
69-
7050
static inline void oidclr(struct object_id *oid)
7151
{
7252
memset(oid->hash, 0, GIT_MAX_RAWSZ);

http-walker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ static int fetch_object(struct walker *walker, unsigned char *hash)
485485

486486
list_for_each(pos, head) {
487487
obj_req = list_entry(pos, struct object_request, node);
488-
if (hasheq(obj_req->oid.hash, hash))
488+
if (hasheq(obj_req->oid.hash, hash, the_repository->hash_algo))
489489
break;
490490
}
491491
if (!obj_req)

match-trees.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ static int splice_tree(const struct object_id *oid1, const char *prefix,
237237
} else {
238238
rewrite_with = oid2;
239239
}
240-
hashcpy(rewrite_here, rewrite_with->hash);
240+
hashcpy(rewrite_here, rewrite_with->hash, the_repository->hash_algo);
241241
status = write_object_file(buf, sz, OBJ_TREE, result);
242242
free(buf);
243243
return status;

0 commit comments

Comments
 (0)