Skip to content

Commit 0578f1e

Browse files
pks-tgitster
authored andcommitted
global: adapt callers to use generic hash context helpers
Adapt callers to use generic hash context helpers instead of using the hash algorithm to update them. This makes the callsites easier to reason about and removes the possibility that the wrong hash algorithm is used to update the hash context's state. And as a nice side effect this also gets rid of a bunch of users of `the_hash_algo`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b2755c1 commit 0578f1e

19 files changed

+103
-107
lines changed

Diff for: builtin/fast-import.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -959,9 +959,9 @@ static int store_object(
959959
hdrlen = format_object_header((char *)hdr, sizeof(hdr), type,
960960
dat->len);
961961
the_hash_algo->init_fn(&c);
962-
the_hash_algo->update_fn(&c, hdr, hdrlen);
963-
the_hash_algo->update_fn(&c, dat->buf, dat->len);
964-
the_hash_algo->final_oid_fn(&oid, &c);
962+
git_hash_update(&c, hdr, hdrlen);
963+
git_hash_update(&c, dat->buf, dat->len);
964+
git_hash_final_oid(&oid, &c);
965965
if (oidout)
966966
oidcpy(oidout, &oid);
967967

@@ -1113,7 +1113,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
11131113
hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len);
11141114

11151115
the_hash_algo->init_fn(&c);
1116-
the_hash_algo->update_fn(&c, out_buf, hdrlen);
1116+
git_hash_update(&c, out_buf, hdrlen);
11171117

11181118
crc32_begin(pack_file);
11191119

@@ -1131,7 +1131,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
11311131
if (!n && feof(stdin))
11321132
die("EOF in data (%" PRIuMAX " bytes remaining)", len);
11331133

1134-
the_hash_algo->update_fn(&c, in_buf, n);
1134+
git_hash_update(&c, in_buf, n);
11351135
s.next_in = in_buf;
11361136
s.avail_in = n;
11371137
len -= n;
@@ -1157,7 +1157,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
11571157
}
11581158
}
11591159
git_deflate_end(&s);
1160-
the_hash_algo->final_oid_fn(&oid, &c);
1160+
git_hash_final_oid(&oid, &c);
11611161

11621162
if (oidout)
11631163
oidcpy(oidout, &oid);

Diff for: builtin/index-pack.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static void flush(void)
301301
if (input_offset) {
302302
if (output_fd >= 0)
303303
write_or_die(output_fd, input_buffer, input_offset);
304-
the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset);
304+
git_hash_update(&input_ctx, input_buffer, input_offset);
305305
memmove(input_buffer, input_buffer + input_offset, input_len);
306306
input_offset = 0;
307307
}
@@ -482,7 +482,7 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
482482
if (!is_delta_type(type)) {
483483
hdrlen = format_object_header(hdr, sizeof(hdr), type, size);
484484
the_hash_algo->init_fn(&c);
485-
the_hash_algo->update_fn(&c, hdr, hdrlen);
485+
git_hash_update(&c, hdr, hdrlen);
486486
} else
487487
oid = NULL;
488488
if (type == OBJ_BLOB && size > big_file_threshold)
@@ -502,7 +502,7 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
502502
status = git_inflate(&stream, 0);
503503
use(input_len - stream.avail_in);
504504
if (oid)
505-
the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out);
505+
git_hash_update(&c, last_out, stream.next_out - last_out);
506506
if (buf == fixed_buf) {
507507
stream.next_out = buf;
508508
stream.avail_out = sizeof(fixed_buf);
@@ -512,7 +512,7 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
512512
bad_object(offset, _("inflate returned %d"), status);
513513
git_inflate_end(&stream);
514514
if (oid)
515-
the_hash_algo->final_oid_fn(oid, &c);
515+
git_hash_final_oid(oid, &c);
516516
return buf == fixed_buf ? NULL : buf;
517517
}
518518

@@ -1286,9 +1286,8 @@ static void parse_pack_objects(unsigned char *hash)
12861286

12871287
/* Check pack integrity */
12881288
flush();
1289-
the_hash_algo->init_fn(&tmp_ctx);
1290-
the_hash_algo->clone_fn(&tmp_ctx, &input_ctx);
1291-
the_hash_algo->final_fn(hash, &tmp_ctx);
1289+
git_hash_clone(&tmp_ctx, &input_ctx);
1290+
git_hash_final(hash, &tmp_ctx);
12921291
if (!hasheq(fill(the_hash_algo->rawsz), hash, the_repository->hash_algo))
12931292
die(_("pack is corrupted (SHA1 mismatch)"));
12941293
use(the_hash_algo->rawsz);

Diff for: builtin/patch-id.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static size_t get_one_patchid(struct object_id *next_oid, struct object_id *resu
8585
!skip_prefix(line, "From ", &p) &&
8686
starts_with(line, "\\ ") && 12 < strlen(line)) {
8787
if (verbatim)
88-
the_hash_algo->update_fn(&ctx, line, strlen(line));
88+
git_hash_update(&ctx, line, strlen(line));
8989
continue;
9090
}
9191

@@ -104,10 +104,10 @@ static size_t get_one_patchid(struct object_id *next_oid, struct object_id *resu
104104
starts_with(line, "Binary files")) {
105105
diff_is_binary = 1;
106106
before = 0;
107-
the_hash_algo->update_fn(&ctx, pre_oid_str,
108-
strlen(pre_oid_str));
109-
the_hash_algo->update_fn(&ctx, post_oid_str,
110-
strlen(post_oid_str));
107+
git_hash_update(&ctx, pre_oid_str,
108+
strlen(pre_oid_str));
109+
git_hash_update(&ctx, post_oid_str,
110+
strlen(post_oid_str));
111111
if (stable)
112112
flush_one_hunk(result, &ctx);
113113
continue;
@@ -165,7 +165,7 @@ static size_t get_one_patchid(struct object_id *next_oid, struct object_id *resu
165165
/* Add line to hash algo (possibly removing whitespace) */
166166
len = verbatim ? strlen(line) : remove_space(line);
167167
patchlen += len;
168-
the_hash_algo->update_fn(&ctx, line, len);
168+
git_hash_update(&ctx, line, len);
169169
}
170170

171171
if (!found_next)

Diff for: builtin/receive-pack.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -572,8 +572,8 @@ static void hmac_hash(unsigned char *out,
572572
memset(key, '\0', GIT_MAX_BLKSZ);
573573
if (the_hash_algo->blksz < key_len) {
574574
the_hash_algo->init_fn(&ctx);
575-
the_hash_algo->update_fn(&ctx, key_in, key_len);
576-
the_hash_algo->final_fn(key, &ctx);
575+
git_hash_update(&ctx, key_in, key_len);
576+
git_hash_final(key, &ctx);
577577
} else {
578578
memcpy(key, key_in, key_len);
579579
}
@@ -586,15 +586,15 @@ static void hmac_hash(unsigned char *out,
586586

587587
/* RFC 2104 2. (3) & (4) */
588588
the_hash_algo->init_fn(&ctx);
589-
the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad));
590-
the_hash_algo->update_fn(&ctx, text, text_len);
591-
the_hash_algo->final_fn(out, &ctx);
589+
git_hash_update(&ctx, k_ipad, sizeof(k_ipad));
590+
git_hash_update(&ctx, text, text_len);
591+
git_hash_final(out, &ctx);
592592

593593
/* RFC 2104 2. (6) & (7) */
594594
the_hash_algo->init_fn(&ctx);
595-
the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad));
596-
the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz);
597-
the_hash_algo->final_fn(out, &ctx);
595+
git_hash_update(&ctx, k_opad, sizeof(k_opad));
596+
git_hash_update(&ctx, out, the_hash_algo->rawsz);
597+
git_hash_final(out, &ctx);
598598
}
599599

600600
static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)

Diff for: builtin/unpack-objects.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void *fill(int min)
7070
if (min > sizeof(buffer))
7171
die("cannot fill %d bytes", min);
7272
if (offset) {
73-
the_hash_algo->update_fn(&ctx, buffer, offset);
73+
git_hash_update(&ctx, buffer, offset);
7474
memmove(buffer, buffer + offset, len);
7575
offset = 0;
7676
}
@@ -667,10 +667,9 @@ int cmd_unpack_objects(int argc,
667667
}
668668
the_hash_algo->init_fn(&ctx);
669669
unpack_all();
670-
the_hash_algo->update_fn(&ctx, buffer, offset);
671-
the_hash_algo->init_fn(&tmp_ctx);
672-
the_hash_algo->clone_fn(&tmp_ctx, &ctx);
673-
the_hash_algo->final_oid_fn(&oid, &tmp_ctx);
670+
git_hash_update(&ctx, buffer, offset);
671+
git_hash_clone(&tmp_ctx, &ctx);
672+
git_hash_final_oid(&oid, &tmp_ctx);
674673
if (strict) {
675674
write_rest();
676675
if (fsck_finish(&fsck_options))

Diff for: bulk-checkin.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static int stream_blob_to_pack(struct bulk_checkin_packfile *state,
194194
if (rsize < hsize)
195195
hsize = rsize;
196196
if (hsize)
197-
the_hash_algo->update_fn(ctx, ibuf, hsize);
197+
git_hash_update(ctx, ibuf, hsize);
198198
*already_hashed_to = offset;
199199
}
200200
s.next_in = ibuf;
@@ -271,7 +271,7 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
271271
header_len = format_object_header((char *)obuf, sizeof(obuf),
272272
OBJ_BLOB, size);
273273
the_hash_algo->init_fn(&ctx);
274-
the_hash_algo->update_fn(&ctx, obuf, header_len);
274+
git_hash_update(&ctx, obuf, header_len);
275275

276276
/* Note: idx is non-NULL when we are writing */
277277
if ((flags & HASH_WRITE_OBJECT) != 0) {
@@ -306,7 +306,7 @@ static int deflate_blob_to_pack(struct bulk_checkin_packfile *state,
306306
if (lseek(fd, seekback, SEEK_SET) == (off_t) -1)
307307
return error("cannot seek back");
308308
}
309-
the_hash_algo->final_oid_fn(result_oid, &ctx);
309+
git_hash_final_oid(result_oid, &ctx);
310310
if (!idx)
311311
return 0;
312312

Diff for: csum-file.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void hashflush(struct hashfile *f)
5050

5151
if (offset) {
5252
if (!f->skip_hash)
53-
f->algop->update_fn(&f->ctx, f->buffer, offset);
53+
git_hash_update(&f->ctx, f->buffer, offset);
5454
flush(f, f->buffer, offset);
5555
f->offset = 0;
5656
}
@@ -73,7 +73,7 @@ int finalize_hashfile(struct hashfile *f, unsigned char *result,
7373
if (f->skip_hash)
7474
hashclr(f->buffer, f->algop);
7575
else
76-
f->algop->final_fn(f->buffer, &f->ctx);
76+
git_hash_final(f->buffer, &f->ctx);
7777

7878
if (result)
7979
hashcpy(result, f->buffer, f->algop);
@@ -128,7 +128,7 @@ void hashwrite(struct hashfile *f, const void *buf, unsigned int count)
128128
* f->offset is necessarily zero.
129129
*/
130130
if (!f->skip_hash)
131-
f->algop->update_fn(&f->ctx, buf, nr);
131+
git_hash_update(&f->ctx, buf, nr);
132132
flush(f, buf, nr);
133133
} else {
134134
/*
@@ -217,7 +217,7 @@ void hashfile_checkpoint(struct hashfile *f, struct hashfile_checkpoint *checkpo
217217
{
218218
hashflush(f);
219219
checkpoint->offset = f->total;
220-
f->algop->clone_fn(&checkpoint->ctx, &f->ctx);
220+
git_hash_clone(&checkpoint->ctx, &f->ctx);
221221
}
222222

223223
int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint)
@@ -228,7 +228,7 @@ int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint
228228
lseek(f->fd, offset, SEEK_SET) != offset)
229229
return -1;
230230
f->total = offset;
231-
f->algop->clone_fn(&f->ctx, &checkpoint->ctx);
231+
git_hash_clone(&f->ctx, &checkpoint->ctx);
232232
f->offset = 0; /* hashflush() was called in checkpoint */
233233
return 0;
234234
}
@@ -256,8 +256,8 @@ int hashfile_checksum_valid(const unsigned char *data, size_t total_len)
256256
return 0; /* say "too short"? */
257257

258258
algop->init_fn(&ctx);
259-
algop->update_fn(&ctx, data, data_len);
260-
algop->final_fn(got, &ctx);
259+
git_hash_update(&ctx, data, data_len);
260+
git_hash_final(got, &ctx);
261261

262262
return hasheq(got, data + data_len, algop);
263263
}

Diff for: diff.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -6415,7 +6415,7 @@ void flush_one_hunk(struct object_id *result, struct git_hash_ctx *ctx)
64156415
unsigned short carry = 0;
64166416
int i;
64176417

6418-
the_hash_algo->final_fn(hash, ctx);
6418+
git_hash_final(hash, ctx);
64196419
the_hash_algo->init_fn(ctx);
64206420
/* 20-byte sum, with carry */
64216421
for (i = 0; i < the_hash_algo->rawsz; ++i) {
@@ -6434,22 +6434,22 @@ static int patch_id_consume(void *priv, char *line, unsigned long len)
64346434
return 0;
64356435
new_len = remove_space(line, len);
64366436

6437-
the_hash_algo->update_fn(data->ctx, line, new_len);
6437+
git_hash_update(data->ctx, line, new_len);
64386438
data->patchlen += new_len;
64396439
return 0;
64406440
}
64416441

64426442
static void patch_id_add_string(struct git_hash_ctx *ctx, const char *str)
64436443
{
6444-
the_hash_algo->update_fn(ctx, str, strlen(str));
6444+
git_hash_update(ctx, str, strlen(str));
64456445
}
64466446

64476447
static void patch_id_add_mode(struct git_hash_ctx *ctx, unsigned mode)
64486448
{
64496449
/* large enough for 2^32 in octal */
64506450
char buf[12];
64516451
int len = xsnprintf(buf, sizeof(buf), "%06o", mode);
6452-
the_hash_algo->update_fn(ctx, buf, len);
6452+
git_hash_update(ctx, buf, len);
64536453
}
64546454

64556455
/* returns 0 upon success, and writes result into oid */
@@ -6493,9 +6493,9 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
64936493
len2 = remove_space(p->two->path, strlen(p->two->path));
64946494
patch_id_add_string(&ctx, "diff--git");
64956495
patch_id_add_string(&ctx, "a/");
6496-
the_hash_algo->update_fn(&ctx, p->one->path, len1);
6496+
git_hash_update(&ctx, p->one->path, len1);
64976497
patch_id_add_string(&ctx, "b/");
6498-
the_hash_algo->update_fn(&ctx, p->two->path, len2);
6498+
git_hash_update(&ctx, p->two->path, len2);
64996499

65006500
if (p->one->mode == 0) {
65016501
patch_id_add_string(&ctx, "newfilemode");
@@ -6514,24 +6514,24 @@ static int diff_get_patch_id(struct diff_options *options, struct object_id *oid
65146514
/* don't do anything since we're only populating header info */
65156515
} else if (diff_filespec_is_binary(options->repo, p->one) ||
65166516
diff_filespec_is_binary(options->repo, p->two)) {
6517-
the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid),
6517+
git_hash_update(&ctx, oid_to_hex(&p->one->oid),
65186518
the_hash_algo->hexsz);
6519-
the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid),
6519+
git_hash_update(&ctx, oid_to_hex(&p->two->oid),
65206520
the_hash_algo->hexsz);
65216521
} else {
65226522
if (p->one->mode == 0) {
65236523
patch_id_add_string(&ctx, "---/dev/null");
65246524
patch_id_add_string(&ctx, "+++b/");
6525-
the_hash_algo->update_fn(&ctx, p->two->path, len2);
6525+
git_hash_update(&ctx, p->two->path, len2);
65266526
} else if (p->two->mode == 0) {
65276527
patch_id_add_string(&ctx, "---a/");
6528-
the_hash_algo->update_fn(&ctx, p->one->path, len1);
6528+
git_hash_update(&ctx, p->one->path, len1);
65296529
patch_id_add_string(&ctx, "+++/dev/null");
65306530
} else {
65316531
patch_id_add_string(&ctx, "---a/");
6532-
the_hash_algo->update_fn(&ctx, p->one->path, len1);
6532+
git_hash_update(&ctx, p->one->path, len1);
65336533
patch_id_add_string(&ctx, "+++b/");
6534-
the_hash_algo->update_fn(&ctx, p->two->path, len2);
6534+
git_hash_update(&ctx, p->two->path, len2);
65356535
}
65366536

65376537
if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||

Diff for: http-push.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,8 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
774774
lock->token = xstrdup(ctx->cdata);
775775

776776
the_hash_algo->init_fn(&hash_ctx);
777-
the_hash_algo->update_fn(&hash_ctx, lock->token, strlen(lock->token));
778-
the_hash_algo->final_fn(lock_token_hash, &hash_ctx);
777+
git_hash_update(&hash_ctx, lock->token, strlen(lock->token));
778+
git_hash_final(lock_token_hash, &hash_ctx);
779779

780780
lock->tmpfile_suffix[0] = '_';
781781
memcpy(lock->tmpfile_suffix + 1, hash_to_hex(lock_token_hash), the_hash_algo->hexsz);

Diff for: http.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2597,8 +2597,8 @@ static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
25972597
freq->stream.next_out = expn;
25982598
freq->stream.avail_out = sizeof(expn);
25992599
freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
2600-
the_hash_algo->update_fn(&freq->c, expn,
2601-
sizeof(expn) - freq->stream.avail_out);
2600+
git_hash_update(&freq->c, expn,
2601+
sizeof(expn) - freq->stream.avail_out);
26022602
} while (freq->stream.avail_in && freq->zret == Z_OK);
26032603
return nmemb;
26042604
}
@@ -2763,7 +2763,7 @@ int finish_http_object_request(struct http_object_request *freq)
27632763
return -1;
27642764
}
27652765

2766-
the_hash_algo->final_oid_fn(&freq->real_oid, &freq->c);
2766+
git_hash_final_oid(&freq->real_oid, &freq->c);
27672767
if (freq->zret != Z_STREAM_END) {
27682768
unlink_or_warn(freq->tmpfile.buf);
27692769
return -1;

0 commit comments

Comments
 (0)