Skip to content

Commit a89fccd

Browse files
rientjesJunio C Hamano
authored and
Junio C Hamano
committed
Do not use memcmp(sha1_1, sha1_2, 20) with hardcoded length.
Introduces global inline: hashcmp(const unsigned char *sha1, const unsigned char *sha2) Uses memcmp for comparison and returns the result based on the length of the hash name (a future runtime decision). Acked-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: David Rientjes <rientjes@google.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent d4baf9e commit a89fccd

31 files changed

+74
-71
lines changed

builtin-commit-tree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static int new_parent(int idx)
6969
int i;
7070
unsigned char *sha1 = parent_sha1[idx];
7171
for (i = 0; i < idx; i++) {
72-
if (!memcmp(parent_sha1[i], sha1, 20)) {
72+
if (!hashcmp(parent_sha1[i], sha1)) {
7373
error("duplicate parent %s ignored", sha1_to_hex(sha1));
7474
return 0;
7575
}

builtin-diff-stages.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void diff_stages(int stage1, int stage2, const char **pathspec)
4646
else if (!two)
4747
diff_addremove(&diff_options, '-', ntohl(one->ce_mode),
4848
one->sha1, name, NULL);
49-
else if (memcmp(one->sha1, two->sha1, 20) ||
49+
else if (hashcmp(one->sha1, two->sha1) ||
5050
(one->ce_mode != two->ce_mode) ||
5151
diff_options.find_copies_harder)
5252
diff_change(&diff_options,

builtin-diff.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void stuff_change(struct diff_options *opt,
6969
struct diff_filespec *one, *two;
7070

7171
if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
72-
!memcmp(old_sha1, new_sha1, 20))
72+
!hashcmp(old_sha1, new_sha1))
7373
return;
7474

7575
if (opt->reverse_diff) {

builtin-pack-objects.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ static int locate_object_entry_hash(const unsigned char *sha1)
441441
memcpy(&ui, sha1, sizeof(unsigned int));
442442
i = ui % object_ix_hashsz;
443443
while (0 < object_ix[i]) {
444-
if (!memcmp(sha1, objects[object_ix[i]-1].sha1, 20))
444+
if (!hashcmp(sha1, objects[object_ix[i] - 1].sha1))
445445
return i;
446446
if (++i == object_ix_hashsz)
447447
i = 0;
@@ -607,7 +607,7 @@ static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)
607607
*/
608608
for (neigh = 0; neigh < 8; neigh++) {
609609
ent = pbase_tree_cache[my_ix];
610-
if (ent && !memcmp(ent->sha1, sha1, 20)) {
610+
if (ent && !hashcmp(ent->sha1, sha1)) {
611611
ent->ref++;
612612
return ent;
613613
}
@@ -789,7 +789,7 @@ static void add_preferred_base(unsigned char *sha1)
789789
return;
790790

791791
for (it = pbase_tree; it; it = it->next) {
792-
if (!memcmp(it->pcache.sha1, tree_sha1, 20)) {
792+
if (!hashcmp(it->pcache.sha1, tree_sha1)) {
793793
free(data);
794794
return;
795795
}
@@ -931,7 +931,7 @@ static struct object_entry **create_sorted_list(entry_sort_t sort)
931931

932932
static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
933933
{
934-
return memcmp(a->sha1, b->sha1, 20);
934+
return hashcmp(a->sha1, b->sha1);
935935
}
936936

937937
static struct object_entry **create_final_object_list(void)

builtin-show-branch.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static int append_head_ref(const char *refname, const unsigned char *sha1)
378378
/* If both heads/foo and tags/foo exists, get_sha1 would
379379
* get confused.
380380
*/
381-
if (get_sha1(refname + ofs, tmp) || memcmp(tmp, sha1, 20))
381+
if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
382382
ofs = 5;
383383
return append_ref(refname + ofs, sha1);
384384
}
@@ -442,7 +442,7 @@ static int rev_is_head(char *head_path, int headlen, char *name,
442442
{
443443
int namelen;
444444
if ((!head_path[0]) ||
445-
(head_sha1 && sha1 && memcmp(head_sha1, sha1, 20)))
445+
(head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
446446
return 0;
447447
namelen = strlen(name);
448448
if ((headlen < namelen) ||

builtin-unpack-objects.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static void added_object(unsigned char *sha1, const char *type, void *data, unsi
136136
struct delta_info *info;
137137

138138
while ((info = *p) != NULL) {
139-
if (!memcmp(info->base_sha1, sha1, 20)) {
139+
if (!hashcmp(info->base_sha1, sha1)) {
140140
*p = info->next;
141141
p = &delta_list;
142142
resolve_delta(type, data, size, info->delta, info->size);
@@ -292,7 +292,7 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
292292
unpack_all();
293293
SHA1_Update(&ctx, buffer, offset);
294294
SHA1_Final(sha1, &ctx);
295-
if (memcmp(fill(20), sha1, 20))
295+
if (hashcmp(fill(20), sha1))
296296
die("final sha1 did not match");
297297
use(20);
298298

builtin-update-index.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static int unresolve_one(const char *path)
378378
ret = -1;
379379
goto free_return;
380380
}
381-
if (!memcmp(ce_2->sha1, ce_3->sha1, 20) &&
381+
if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
382382
ce_2->ce_mode == ce_3->ce_mode) {
383383
fprintf(stderr, "%s: identical in both, skipping.\n",
384384
path);
@@ -460,7 +460,7 @@ static int do_reupdate(int ac, const char **av,
460460
old = read_one_ent(NULL, head_sha1,
461461
ce->name, ce_namelen(ce), 0);
462462
if (old && ce->ce_mode == old->ce_mode &&
463-
!memcmp(ce->sha1, old->sha1, 20)) {
463+
!hashcmp(ce->sha1, old->sha1)) {
464464
free(old);
465465
continue; /* unchanged */
466466
}

cache.h

+4
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ static inline int is_null_sha1(const unsigned char *sha1)
214214
{
215215
return !memcmp(sha1, null_sha1, 20);
216216
}
217+
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
218+
{
219+
return memcmp(sha1, sha2, 20);
220+
}
217221

218222
int git_mkstemp(char *path, size_t n, const char *template);
219223

combine-diff.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,
688688
for (i = 0; i < num_parent; i++) {
689689
int j;
690690
for (j = 0; j < i; j++) {
691-
if (!memcmp(elem->parent[i].sha1,
692-
elem->parent[j].sha1, 20)) {
691+
if (!hashcmp(elem->parent[i].sha1,
692+
elem->parent[j].sha1)) {
693693
reuse_combine_diff(sline, cnt, i, j);
694694
break;
695695
}

commit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static int commit_graft_pos(const unsigned char *sha1)
123123
while (lo < hi) {
124124
int mi = (lo + hi) / 2;
125125
struct commit_graft *graft = commit_graft[mi];
126-
int cmp = memcmp(sha1, graft->sha1, 20);
126+
int cmp = hashcmp(sha1, graft->sha1);
127127
if (!cmp)
128128
return mi;
129129
if (cmp < 0)

convert-objects.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static struct entry *lookup_entry(unsigned char *sha1)
3939
while (low < high) {
4040
int next = (low + high) / 2;
4141
struct entry *n = convert[next];
42-
int cmp = memcmp(sha1, n->old_sha1, 20);
42+
int cmp = hashcmp(sha1, n->old_sha1);
4343
if (!cmp)
4444
return n;
4545
if (cmp < 0) {

diff-lib.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static int show_modified(struct rev_info *revs,
215215
}
216216

217217
oldmode = old->ce_mode;
218-
if (mode == oldmode && !memcmp(sha1, old->sha1, 20) &&
218+
if (mode == oldmode && !hashcmp(sha1, old->sha1) &&
219219
!revs->diffopt.find_copies_harder)
220220
return 0;
221221

diff.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ static int work_tree_matches(const char *name, const unsigned char *sha1)
11401140
if ((lstat(name, &st) < 0) ||
11411141
!S_ISREG(st.st_mode) || /* careful! */
11421142
ce_match_stat(ce, &st, 0) ||
1143-
memcmp(sha1, ce->sha1, 20))
1143+
hashcmp(sha1, ce->sha1))
11441144
return 0;
11451145
/* we return 1 only when we can stat, it is a regular file,
11461146
* stat information matches, and sha1 recorded in the cache
@@ -1168,7 +1168,7 @@ static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
11681168
while (last > first) {
11691169
int cmp, next = (last + first) >> 1;
11701170
e = sha1_size_cache[next];
1171-
cmp = memcmp(e->sha1, sha1, 20);
1171+
cmp = hashcmp(e->sha1, sha1);
11721172
if (!cmp)
11731173
return e;
11741174
if (cmp < 0) {
@@ -1579,7 +1579,7 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
15791579
;
15801580
}
15811581

1582-
if (memcmp(one->sha1, two->sha1, 20)) {
1582+
if (hashcmp(one->sha1, two->sha1)) {
15831583
int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
15841584

15851585
len += snprintf(msg + len, sizeof(msg) - len,
@@ -2098,7 +2098,7 @@ int diff_unmodified_pair(struct diff_filepair *p)
20982098
* dealing with a change.
20992099
*/
21002100
if (one->sha1_valid && two->sha1_valid &&
2101-
!memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
2101+
!hashcmp(one->sha1, two->sha1))
21022102
return 1; /* no change */
21032103
if (!one->sha1_valid && !two->sha1_valid)
21042104
return 1; /* both look at the same file on the filesystem. */
@@ -2237,7 +2237,7 @@ static void diff_resolve_rename_copy(void)
22372237
if (!p->status)
22382238
p->status = DIFF_STATUS_RENAMED;
22392239
}
2240-
else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
2240+
else if (hashcmp(p->one->sha1, p->two->sha1) ||
22412241
p->one->mode != p->two->mode)
22422242
p->status = DIFF_STATUS_MODIFIED;
22432243
else {

diffcore-break.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static int should_break(struct diff_filespec *src,
5656
return 0; /* leave symlink rename alone */
5757

5858
if (src->sha1_valid && dst->sha1_valid &&
59-
!memcmp(src->sha1, dst->sha1, 20))
59+
!hashcmp(src->sha1, dst->sha1))
6060
return 0; /* they are the same */
6161

6262
if (diff_populate_filespec(src, 0) || diff_populate_filespec(dst, 0))

diffcore-rename.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int is_exact_match(struct diff_filespec *src,
101101
int contents_too)
102102
{
103103
if (src->sha1_valid && dst->sha1_valid &&
104-
!memcmp(src->sha1, dst->sha1, 20))
104+
!hashcmp(src->sha1, dst->sha1))
105105
return 1;
106106
if (!contents_too)
107107
return 0;

dump-cache-tree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static int dump_cache_tree(struct cache_tree *it,
3333
}
3434
else {
3535
dump_one(it, pfx, "");
36-
if (memcmp(it->sha1, ref->sha1, 20) ||
36+
if (hashcmp(it->sha1, ref->sha1) ||
3737
ref->entry_count != it->entry_count ||
3838
ref->subtree_nr != it->subtree_nr) {
3939
dump_one(ref, pfx, "#(ref) ");

http-fetch.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static void finish_object_request(struct object_request *obj_req)
301301
unlink(obj_req->tmpfile);
302302
return;
303303
}
304-
if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
304+
if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
305305
unlink(obj_req->tmpfile);
306306
return;
307307
}
@@ -1070,7 +1070,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
10701070
int ret = 0;
10711071
struct object_request *obj_req = object_queue_head;
10721072

1073-
while (obj_req != NULL && memcmp(obj_req->sha1, sha1, 20))
1073+
while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
10741074
obj_req = obj_req->next;
10751075
if (obj_req == NULL)
10761076
return error("Couldn't find request for %s in the queue", hex);
@@ -1109,7 +1109,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
11091109
} else if (obj_req->zret != Z_STREAM_END) {
11101110
corrupt_object_found++;
11111111
ret = error("File %s (%s) corrupt", hex, obj_req->url);
1112-
} else if (memcmp(obj_req->sha1, obj_req->real_sha1, 20)) {
1112+
} else if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
11131113
ret = error("File %s has bad hash", hex);
11141114
} else if (obj_req->rename < 0) {
11151115
ret = error("unable to write sha1 filename %s",

http-push.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ static void finish_request(struct transfer_request *request)
745745
SHA1_Final(request->real_sha1, &request->c);
746746
if (request->zret != Z_STREAM_END) {
747747
unlink(request->tmpfile);
748-
} else if (memcmp(request->obj->sha1, request->real_sha1, 20)) {
748+
} else if (hashcmp(request->obj->sha1, request->real_sha1)) {
749749
unlink(request->tmpfile);
750750
} else {
751751
request->rename =
@@ -2416,7 +2416,7 @@ int main(int argc, char **argv)
24162416

24172417
if (!ref->peer_ref)
24182418
continue;
2419-
if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
2419+
if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
24202420
if (push_verbosely || 1)
24212421
fprintf(stderr, "'%s': up-to-date\n", ref->name);
24222422
continue;

index-pack.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static void parse_pack_header(void)
8282
SHA1_Init(&ctx);
8383
SHA1_Update(&ctx, pack_base, pack_size - 20);
8484
SHA1_Final(sha1, &ctx);
85-
if (memcmp(sha1, pack_base + pack_size - 20, 20))
85+
if (hashcmp(sha1, pack_base + pack_size - 20))
8686
die("packfile '%s' SHA1 mismatch", pack_name);
8787
}
8888

@@ -189,7 +189,7 @@ static int find_delta(const unsigned char *base_sha1)
189189
struct delta_entry *delta = &deltas[next];
190190
int cmp;
191191

192-
cmp = memcmp(base_sha1, delta->base_sha1, 20);
192+
cmp = hashcmp(base_sha1, delta->base_sha1);
193193
if (!cmp)
194194
return next;
195195
if (cmp < 0) {
@@ -210,9 +210,9 @@ static int find_deltas_based_on_sha1(const unsigned char *base_sha1,
210210

211211
if (first < 0)
212212
return -1;
213-
while (first > 0 && !memcmp(deltas[first-1].base_sha1, base_sha1, 20))
213+
while (first > 0 && !hashcmp(deltas[first - 1].base_sha1, base_sha1))
214214
--first;
215-
while (last < end && !memcmp(deltas[last+1].base_sha1, base_sha1, 20))
215+
while (last < end && !hashcmp(deltas[last + 1].base_sha1, base_sha1))
216216
++last;
217217
*first_index = first;
218218
*last_index = last;
@@ -278,7 +278,7 @@ static int compare_delta_entry(const void *a, const void *b)
278278
{
279279
const struct delta_entry *delta_a = a;
280280
const struct delta_entry *delta_b = b;
281-
return memcmp(delta_a->base_sha1, delta_b->base_sha1, 20);
281+
return hashcmp(delta_a->base_sha1, delta_b->base_sha1);
282282
}
283283

284284
static void parse_pack_objects(void)
@@ -350,7 +350,7 @@ static int sha1_compare(const void *_a, const void *_b)
350350
{
351351
struct object_entry *a = *(struct object_entry **)_a;
352352
struct object_entry *b = *(struct object_entry **)_b;
353-
return memcmp(a->sha1, b->sha1, 20);
353+
return hashcmp(a->sha1, b->sha1);
354354
}
355355

356356
static void write_index_file(const char *index_name, unsigned char *sha1)

merge-tree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static int same_entry(struct name_entry *a, struct name_entry *b)
152152
{
153153
return a->sha1 &&
154154
b->sha1 &&
155-
!memcmp(a->sha1, b->sha1, 20) &&
155+
!hashcmp(a->sha1, b->sha1) &&
156156
a->mode == b->mode;
157157
}
158158

object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct object *lookup_object(const unsigned char *sha1)
5858

5959
i = hashtable_index(sha1);
6060
while ((obj = obj_hash[i]) != NULL) {
61-
if (!memcmp(sha1, obj->sha1, 20))
61+
if (!hashcmp(sha1, obj->sha1))
6262
break;
6363
i++;
6464
if (i == obj_hash_size)

pack-check.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ static int verify_packfile(struct packed_git *p)
2929
pack_base = p->pack_base;
3030
SHA1_Update(&ctx, pack_base, pack_size - 20);
3131
SHA1_Final(sha1, &ctx);
32-
if (memcmp(sha1, (char *) pack_base + pack_size - 20, 20))
32+
if (hashcmp(sha1, (unsigned char *)pack_base + pack_size - 20))
3333
return error("Packfile %s SHA1 mismatch with itself",
3434
p->pack_name);
35-
if (memcmp(sha1, (char *) index_base + index_size - 40, 20))
35+
if (hashcmp(sha1, (unsigned char *)index_base + index_size - 40))
3636
return error("Packfile %s SHA1 mismatch with idx",
3737
p->pack_name);
3838

@@ -135,7 +135,7 @@ int verify_pack(struct packed_git *p, int verbose)
135135
SHA1_Init(&ctx);
136136
SHA1_Update(&ctx, index_base, index_size - 20);
137137
SHA1_Final(sha1, &ctx);
138-
if (memcmp(sha1, (char *) index_base + index_size - 20, 20))
138+
if (hashcmp(sha1, (unsigned char *)index_base + index_size - 20))
139139
ret = error("Packfile index for %s SHA1 mismatch",
140140
p->pack_name);
141141

0 commit comments

Comments
 (0)