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;

notes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static struct leaf_node *note_tree_find(struct notes_tree *t,
149149
void **p = note_tree_search(t, &tree, &n, key_sha1);
150150
if (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {
151151
struct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);
152-
if (hasheq(key_sha1, l->key_oid.hash))
152+
if (hasheq(key_sha1, l->key_oid.hash, the_repository->hash_algo))
153153
return l;
154154
}
155155
return NULL;

pack-bitmap-write.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ static void write_hash_cache(struct hashfile *f,
790790
void bitmap_writer_set_checksum(struct bitmap_writer *writer,
791791
const unsigned char *sha1)
792792
{
793-
hashcpy(writer->pack_checksum, sha1);
793+
hashcpy(writer->pack_checksum, sha1, the_repository->hash_algo);
794794
}
795795

796796
void bitmap_writer_finish(struct bitmap_writer *writer,
@@ -816,7 +816,7 @@ void bitmap_writer_finish(struct bitmap_writer *writer,
816816
header.version = htons(default_version);
817817
header.options = htons(flags | options);
818818
header.entry_count = htonl(writer->selected_nr);
819-
hashcpy(header.checksum, writer->pack_checksum);
819+
hashcpy(header.checksum, writer->pack_checksum, the_repository->hash_algo);
820820

821821
hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
822822
dump_bitmap(f, writer->commits);

pack-bitmap.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
367367
if (load_bitmap_header(bitmap_git) < 0)
368368
goto cleanup;
369369

370-
if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
370+
if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum,
371+
the_repository->hash_algo)) {
371372
error(_("checksum doesn't match in MIDX and bitmap"));
372373
goto cleanup;
373374
}

pack-check.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ static int verify_packfile(struct repository *r,
7878
} while (offset < pack_sig_ofs);
7979
r->hash_algo->final_fn(hash, &ctx);
8080
pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
81-
if (!hasheq(hash, pack_sig))
81+
if (!hasheq(hash, pack_sig, the_repository->hash_algo))
8282
err = error("%s pack checksum mismatch",
8383
p->pack_name);
84-
if (!hasheq(index_base + index_size - r->hash_algo->hexsz, pack_sig))
84+
if (!hasheq(index_base + index_size - r->hash_algo->hexsz, pack_sig,
85+
the_repository->hash_algo))
8586
err = error("%s pack checksum does not match its index",
8687
p->pack_name);
8788
unuse_pack(w_curs);

pack-write.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ void fixup_pack_header_footer(int pack_fd,
428428
if (partial_pack_offset == 0) {
429429
unsigned char hash[GIT_MAX_RAWSZ];
430430
the_hash_algo->final_fn(hash, &old_hash_ctx);
431-
if (!hasheq(hash, partial_pack_hash))
431+
if (!hasheq(hash, partial_pack_hash,
432+
the_repository->hash_algo))
432433
die("Unexpected checksum for %s "
433434
"(disk corruption?)", pack_name);
434435
/*

packfile.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
242242
struct packed_git *p = alloc_packed_git(alloc);
243243

244244
memcpy(p->pack_name, path, alloc); /* includes NUL */
245-
hashcpy(p->hash, sha1);
245+
hashcpy(p->hash, sha1, the_repository->hash_algo);
246246
if (check_packed_git_idx(idx_path, p)) {
247247
free(p);
248248
return NULL;
@@ -596,7 +596,7 @@ static int open_packed_git_1(struct packed_git *p)
596596
if (read_result != hashsz)
597597
return error("packfile %s signature is unavailable", p->pack_name);
598598
idx_hash = ((unsigned char *)p->index_data) + p->index_size - hashsz * 2;
599-
if (!hasheq(hash, idx_hash))
599+
if (!hasheq(hash, idx_hash, the_repository->hash_algo))
600600
return error("packfile %s does not match index", p->pack_name);
601601
return 0;
602602
}
@@ -751,7 +751,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)
751751
p->mtime = st.st_mtime;
752752
if (path_len < the_hash_algo->hexsz ||
753753
get_hash_hex(path + path_len - the_hash_algo->hexsz, p->hash))
754-
hashclr(p->hash);
754+
hashclr(p->hash, the_repository->hash_algo);
755755
return p;
756756
}
757757

@@ -1971,7 +1971,7 @@ off_t find_pack_entry_one(const unsigned char *sha1,
19711971
return 0;
19721972
}
19731973

1974-
hashcpy(oid.hash, sha1);
1974+
hashcpy(oid.hash, sha1, the_repository->hash_algo);
19751975
if (bsearch_pack(&oid, p, &result))
19761976
return nth_packed_object_offset(p, result);
19771977
return 0;

read-cache.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ static int verify_hdr(const struct cache_header *hdr, unsigned long size)
17351735
the_hash_algo->init_fn(&c);
17361736
the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz);
17371737
the_hash_algo->final_fn(hash, &c);
1738-
if (!hasheq(hash, start))
1738+
if (!hasheq(hash, start, the_repository->hash_algo))
17391739
return error(_("bad index file sha1 signature"));
17401740
return 0;
17411741
}
@@ -2641,7 +2641,7 @@ static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
26412641
ondisk->uid = htonl(ce->ce_stat_data.sd_uid);
26422642
ondisk->gid = htonl(ce->ce_stat_data.sd_gid);
26432643
ondisk->size = htonl(ce->ce_stat_data.sd_size);
2644-
hashcpy(ondisk->data, ce->oid.hash);
2644+
hashcpy(ondisk->data, ce->oid.hash, the_repository->hash_algo);
26452645

26462646
flags = ce->ce_flags & ~CE_NAMEMASK;
26472647
flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce));
@@ -2730,7 +2730,7 @@ static int verify_index_from(const struct index_state *istate, const char *path)
27302730
if (n != the_hash_algo->rawsz)
27312731
goto out;
27322732

2733-
if (!hasheq(istate->oid.hash, hash))
2733+
if (!hasheq(istate->oid.hash, hash, the_repository->hash_algo))
27342734
goto out;
27352735

27362736
close(fd);
@@ -3603,7 +3603,7 @@ static size_t read_eoie_extension(const char *mmap, size_t mmap_size)
36033603
src_offset += extsize;
36043604
}
36053605
the_hash_algo->final_fn(hash, &c);
3606-
if (!hasheq(hash, (const unsigned char *)index))
3606+
if (!hasheq(hash, (const unsigned char *)index, the_repository->hash_algo))
36073607
return 0;
36083608

36093609
/* Validate that the extension offsets returned us back to the eoie extension. */

0 commit comments

Comments
 (0)