Skip to content

Commit a89fccd

Browse files
rientjesJunio C Hamano
authored andcommitted
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 <[email protected]> Signed-off-by: David Rientjes <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d4baf9e commit a89fccd

31 files changed

+74
-71
lines changed

builtin-commit-tree.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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)

0 commit comments

Comments
 (0)