@matheusbsbs / matheus-bagliano / commits / 99a19b4302
Fork 0

Make sure get_sha1 does not accept ambiguous sha1 prefix.

The original code did not even check alternates, and was confused if an unpacked object was uniquely found when there was another object that shares the same prefix in the pack. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 2, 2005 at 21:40 UTC 99a19b43021024e12ddb67831f8e5919f86a5979
1 file changed +68 -21
sha1_name.c
+68 -21
@@ -3,31 +3,49 @@
3
4 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
5 {
6 - static char dirname[PATH_MAX];
6 + struct alternate_object_database *alt;
7 char hex[40];
8 - DIR *dir;
9 - int found;
8 + int found = 0;
9 + static struct alternate_object_database *fakeent;
10 +
11 + if (!fakeent) {
12 + const char *objdir = get_object_directory();
13 + int objdir_len = strlen(objdir);
14 + int entlen = objdir_len + 43;
15 + fakeent = xmalloc(sizeof(*fakeent) + entlen);
16 + memcpy(fakeent->base, objdir, objdir_len);
17 + fakeent->name = fakeent->base + objdir_len + 1;
18 + fakeent->name[-1] = '/';
19 + }
20 + fakeent->next = alt_odb_list;
21
11 - snprintf(dirname, sizeof(dirname), "%s/%.2s", get_object_directory(), name);
12 - dir = opendir(dirname);
22 sprintf(hex, "%.2s", name);
14 - found = 0;
15 - if (dir) {
23 + for (alt = fakeent; alt && found < 2; alt = alt->next) {
24 struct dirent *de;
25 + DIR *dir;
26 + sprintf(alt->name, "%.2s/", name);
27 + dir = opendir(alt->base);
28 + if (!dir)
29 + continue;
30 while ((de = readdir(dir)) != NULL) {
31 if (strlen(de->d_name) != 38)
32 continue;
20 - if (memcmp(de->d_name, name + 2, len-2))
33 + if (memcmp(de->d_name, name + 2, len - 2))
34 continue;
22 - memcpy(hex + 2, de->d_name, 38);
23 - if (++found > 1)
35 + if (!found) {
36 + memcpy(hex + 2, de->d_name, 38);
37 + found++;
38 + }
39 + else if (memcmp(hex + 2, de->d_name, 38)) {
40 + found = 2;
41 break;
42 + }
43 }
44 closedir(dir);
45 }
46 if (found == 1)
47 return get_sha1_hex(hex, sha1) == 0;
30 - return 0;
48 + return found;
49 }
50
51 static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
@@ -48,9 +66,11 @@ static int match_sha(unsigned len, const unsigned char *a, const unsigned char *
66 static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
67 {
68 struct packed_git *p;
69 + unsigned char found_sha1[20];
70 + int found = 0;
71
72 prepare_packed_git();
53 - for (p = packed_git; p; p = p->next) {
73 + for (p = packed_git; p && found < 2; p = p->next) {
74 unsigned num = num_packed_objects(p);
75 unsigned first = 0, last = num;
76 while (first < last) {
@@ -71,16 +91,45 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
91 last = mid;
92 }
93 if (first < num) {
74 - unsigned char now[20], next[20];
94 + unsigned char now[20];
95 nth_packed_object_sha1(p, first, now);
96 if (match_sha(len, match, now)) {
77 - if (nth_packed_object_sha1(p, first+1, next) || !match_sha(len, match, next)) {
78 - memcpy(sha1, now, 20);
79 - return 1;
97 + if (!found) {
98 + memcpy(found_sha1, now, 20);
99 + found++;
100 + }
101 + else if (memcmp(found_sha1, now, 20)) {
102 + found = 2;
103 + break;
104 }
105 }
106 }
107 }
108 + if (found == 1)
109 + memcpy(sha1, found_sha1, 20);
110 + return found;
111 +}
112 +
113 +static int find_unique_short_object(int len, char *canonical,
114 + unsigned char *res, unsigned char *sha1)
115 +{
116 + int has_unpacked, has_packed;
117 + unsigned char unpacked_sha1[20], packed_sha1[20];
118 +
119 + has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
120 + has_packed = find_short_packed_object(len, res, packed_sha1);
121 + if (!has_unpacked && !has_packed)
122 + return -1;
123 + if (1 < has_unpacked || 1 < has_packed)
124 + return -1;
125 + if (has_unpacked != has_packed) {
126 + memcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1), 20);
127 + return 0;
128 + }
129 + /* Both have unique ones -- do they match? */
130 + if (memcmp(packed_sha1, unpacked_sha1, 20))
131 + return -1;
132 + memcpy(sha1, packed_sha1, 20);
133 return 0;
134 }
135
@@ -112,11 +161,8 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1)
161 val <<= 4;
162 res[i >> 1] |= val;
163 }
115 - if (find_short_object_filename(i, canonical, sha1))
116 - return 0;
117 - if (find_short_packed_object(i, res, sha1))
118 - return 0;
119 - return -1;
164 +
165 + return find_unique_short_object(i, canonical, res, sha1);
166 }
167
168 static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
@@ -246,5 +292,6 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
292 */
293 int get_sha1(const char *name, unsigned char *sha1)
294 {
295 + prepare_alt_odb();
296 return get_sha1_1(name, strlen(name), sha1);
297 }