]> git.ipfire.org Git - thirdparty/git.git/blame - fsck.c
Make sure fsck_commit_buffer() does not run out of the buffer
[thirdparty/git.git] / fsck.c
CommitLineData
355885d5
MK
1#include "cache.h"
2#include "object.h"
3#include "blob.h"
4#include "tree.h"
5#include "tree-walk.h"
6#include "commit.h"
7#include "tag.h"
8#include "fsck.h"
9
10static int fsck_walk_tree(struct tree *tree, fsck_walk_func walk, void *data)
11{
12 struct tree_desc desc;
13 struct name_entry entry;
14 int res = 0;
15
16 if (parse_tree(tree))
17 return -1;
18
19 init_tree_desc(&desc, tree->buffer, tree->size);
20 while (tree_entry(&desc, &entry)) {
21 int result;
22
23 if (S_ISGITLINK(entry.mode))
24 continue;
25 if (S_ISDIR(entry.mode))
26 result = walk(&lookup_tree(entry.sha1)->object, OBJ_TREE, data);
27 else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode))
28 result = walk(&lookup_blob(entry.sha1)->object, OBJ_BLOB, data);
29 else {
82247e9b 30 result = error("in tree %s: entry %s has bad mode %.6o",
355885d5
MK
31 sha1_to_hex(tree->object.sha1), entry.path, entry.mode);
32 }
33 if (result < 0)
34 return result;
35 if (!res)
36 res = result;
37 }
38 return res;
39}
40
41static int fsck_walk_commit(struct commit *commit, fsck_walk_func walk, void *data)
42{
43 struct commit_list *parents;
44 int res;
45 int result;
46
47 if (parse_commit(commit))
48 return -1;
49
50 result = walk((struct object *)commit->tree, OBJ_TREE, data);
51 if (result < 0)
52 return result;
53 res = result;
54
55 parents = commit->parents;
56 while (parents) {
57 result = walk((struct object *)parents->item, OBJ_COMMIT, data);
58 if (result < 0)
59 return result;
60 if (!res)
61 res = result;
62 parents = parents->next;
63 }
64 return res;
65}
66
67static int fsck_walk_tag(struct tag *tag, fsck_walk_func walk, void *data)
68{
69 if (parse_tag(tag))
70 return -1;
71 return walk(tag->tagged, OBJ_ANY, data);
72}
73
74int fsck_walk(struct object *obj, fsck_walk_func walk, void *data)
75{
76 if (!obj)
77 return -1;
78 switch (obj->type) {
79 case OBJ_BLOB:
80 return 0;
81 case OBJ_TREE:
82 return fsck_walk_tree((struct tree *)obj, walk, data);
83 case OBJ_COMMIT:
84 return fsck_walk_commit((struct commit *)obj, walk, data);
85 case OBJ_TAG:
86 return fsck_walk_tag((struct tag *)obj, walk, data);
87 default:
88 error("Unknown object type for %s", sha1_to_hex(obj->sha1));
89 return -1;
90 }
91}
ba002f3b
MK
92
93/*
94 * The entries in a tree are ordered in the _path_ order,
95 * which means that a directory entry is ordered by adding
96 * a slash to the end of it.
97 *
98 * So a directory called "a" is ordered _after_ a file
99 * called "a.c", because "a/" sorts after "a.c".
100 */
101#define TREE_UNORDERED (-1)
102#define TREE_HAS_DUPS (-2)
103
104static int verify_ordered(unsigned mode1, const char *name1, unsigned mode2, const char *name2)
105{
106 int len1 = strlen(name1);
107 int len2 = strlen(name2);
108 int len = len1 < len2 ? len1 : len2;
109 unsigned char c1, c2;
110 int cmp;
111
112 cmp = memcmp(name1, name2, len);
113 if (cmp < 0)
114 return 0;
115 if (cmp > 0)
116 return TREE_UNORDERED;
117
118 /*
119 * Ok, the first <len> characters are the same.
120 * Now we need to order the next one, but turn
121 * a '\0' into a '/' for a directory entry.
122 */
123 c1 = name1[len];
124 c2 = name2[len];
125 if (!c1 && !c2)
126 /*
127 * git-write-tree used to write out a nonsense tree that has
128 * entries with the same name, one blob and one tree. Make
129 * sure we do not have duplicate entries.
130 */
131 return TREE_HAS_DUPS;
132 if (!c1 && S_ISDIR(mode1))
133 c1 = '/';
134 if (!c2 && S_ISDIR(mode2))
135 c2 = '/';
136 return c1 < c2 ? 0 : TREE_UNORDERED;
137}
138
139static int fsck_tree(struct tree *item, int strict, fsck_error error_func)
140{
141 int retval;
c479d14a 142 int has_null_sha1 = 0;
ba002f3b
MK
143 int has_full_path = 0;
144 int has_empty_name = 0;
5d34a435
JK
145 int has_dot = 0;
146 int has_dotdot = 0;
5c17f512 147 int has_dotgit = 0;
ba002f3b
MK
148 int has_zero_pad = 0;
149 int has_bad_modes = 0;
150 int has_dup_entries = 0;
151 int not_properly_sorted = 0;
152 struct tree_desc desc;
153 unsigned o_mode;
154 const char *o_name;
ba002f3b
MK
155
156 init_tree_desc(&desc, item->buffer, item->size);
157
158 o_mode = 0;
159 o_name = NULL;
ba002f3b
MK
160
161 while (desc.size) {
162 unsigned mode;
163 const char *name;
c479d14a 164 const unsigned char *sha1;
ba002f3b 165
c479d14a 166 sha1 = tree_entry_extract(&desc, &name, &mode);
ba002f3b 167
effd12ec
HS
168 has_null_sha1 |= is_null_sha1(sha1);
169 has_full_path |= !!strchr(name, '/');
170 has_empty_name |= !*name;
171 has_dot |= !strcmp(name, ".");
172 has_dotdot |= !strcmp(name, "..");
173 has_dotgit |= !strcmp(name, ".git");
ba002f3b
MK
174 has_zero_pad |= *(char *)desc.buffer == '0';
175 update_tree_entry(&desc);
176
177 switch (mode) {
178 /*
179 * Standard modes..
180 */
181 case S_IFREG | 0755:
182 case S_IFREG | 0644:
183 case S_IFLNK:
184 case S_IFDIR:
185 case S_IFGITLINK:
186 break;
187 /*
188 * This is nonstandard, but we had a few of these
189 * early on when we honored the full set of mode
190 * bits..
191 */
192 case S_IFREG | 0664:
193 if (!strict)
194 break;
195 default:
196 has_bad_modes = 1;
197 }
198
199 if (o_name) {
200 switch (verify_ordered(o_mode, o_name, mode, name)) {
201 case TREE_UNORDERED:
202 not_properly_sorted = 1;
203 break;
204 case TREE_HAS_DUPS:
205 has_dup_entries = 1;
206 break;
207 default:
208 break;
209 }
210 }
211
212 o_mode = mode;
213 o_name = name;
ba002f3b
MK
214 }
215
216 retval = 0;
c479d14a
JK
217 if (has_null_sha1)
218 retval += error_func(&item->object, FSCK_WARN, "contains entries pointing to null sha1");
ba002f3b
MK
219 if (has_full_path)
220 retval += error_func(&item->object, FSCK_WARN, "contains full pathnames");
221 if (has_empty_name)
222 retval += error_func(&item->object, FSCK_WARN, "contains empty pathname");
5d34a435
JK
223 if (has_dot)
224 retval += error_func(&item->object, FSCK_WARN, "contains '.'");
225 if (has_dotdot)
226 retval += error_func(&item->object, FSCK_WARN, "contains '..'");
5c17f512
JK
227 if (has_dotgit)
228 retval += error_func(&item->object, FSCK_WARN, "contains '.git'");
ba002f3b
MK
229 if (has_zero_pad)
230 retval += error_func(&item->object, FSCK_WARN, "contains zero-padded file modes");
231 if (has_bad_modes)
232 retval += error_func(&item->object, FSCK_WARN, "contains bad file modes");
233 if (has_dup_entries)
234 retval += error_func(&item->object, FSCK_ERROR, "contains duplicate file entries");
235 if (not_properly_sorted)
236 retval += error_func(&item->object, FSCK_ERROR, "not properly sorted");
237 return retval;
238}
239
4d0d8975
JS
240static int require_end_of_header(const void *data, unsigned long size,
241 struct object *obj, fsck_error error_func)
242{
243 const char *buffer = (const char *)data;
244 unsigned long i;
245
246 for (i = 0; i < size; i++) {
247 switch (buffer[i]) {
248 case '\0':
249 return error_func(obj, FSCK_ERROR,
250 "unterminated header: NUL at offset %d", i);
251 case '\n':
252 if (i + 1 < size && buffer[i + 1] == '\n')
253 return 0;
254 }
255 }
256
257 return error_func(obj, FSCK_ERROR, "unterminated header");
258}
259
de42180f 260static int fsck_ident(const char **ident, struct object *obj, fsck_error error_func)
daae1922 261{
d4b8de04
JK
262 char *end;
263
53f53cff 264 if (**ident == '<')
daae1922 265 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
53f53cff
DI
266 *ident += strcspn(*ident, "<>\n");
267 if (**ident == '>')
268 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad name");
daae1922
JN
269 if (**ident != '<')
270 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing email");
53f53cff
DI
271 if ((*ident)[-1] != ' ')
272 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before email");
daae1922
JN
273 (*ident)++;
274 *ident += strcspn(*ident, "<>\n");
275 if (**ident != '>')
276 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad email");
277 (*ident)++;
278 if (**ident != ' ')
279 return error_func(obj, FSCK_ERROR, "invalid author/committer line - missing space before date");
280 (*ident)++;
281 if (**ident == '0' && (*ident)[1] != ' ')
282 return error_func(obj, FSCK_ERROR, "invalid author/committer line - zero-padded date");
7ca36d93 283 if (date_overflows(strtoul(*ident, &end, 10)))
d4b8de04
JK
284 return error_func(obj, FSCK_ERROR, "invalid author/committer line - date causes integer overflow");
285 if (end == *ident || *end != ' ')
daae1922 286 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad date");
d4b8de04 287 *ident = end + 1;
daae1922
JN
288 if ((**ident != '+' && **ident != '-') ||
289 !isdigit((*ident)[1]) ||
290 !isdigit((*ident)[2]) ||
291 !isdigit((*ident)[3]) ||
292 !isdigit((*ident)[4]) ||
293 ((*ident)[5] != '\n'))
294 return error_func(obj, FSCK_ERROR, "invalid author/committer line - bad time zone");
295 (*ident) += 6;
296 return 0;
297}
298
bc6b8fc1 299static int fsck_commit_buffer(struct commit *commit, const char *buffer,
90a398bb 300 unsigned long size, fsck_error error_func)
ba002f3b 301{
ba002f3b
MK
302 unsigned char tree_sha1[20], sha1[20];
303 struct commit_graft *graft;
9d02150c 304 unsigned parent_count, parent_line_count = 0;
daae1922 305 int err;
ba002f3b 306
4d0d8975
JS
307 if (require_end_of_header(buffer, size, &commit->object, error_func))
308 return -1;
309
cf4fff57 310 if (!skip_prefix(buffer, "tree ", &buffer))
ba002f3b 311 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
2d820a61 312 if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n')
ba002f3b 313 return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");
2d820a61 314 buffer += 41;
cf4fff57 315 while (skip_prefix(buffer, "parent ", &buffer)) {
2d820a61 316 if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
ba002f3b 317 return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
2d820a61 318 buffer += 41;
9d02150c 319 parent_line_count++;
ba002f3b
MK
320 }
321 graft = lookup_commit_graft(commit->object.sha1);
9d02150c 322 parent_count = commit_list_count(commit->parents);
ba002f3b 323 if (graft) {
9d02150c 324 if (graft->nr_parent == -1 && !parent_count)
ba002f3b 325 ; /* shallow commit */
9d02150c 326 else if (graft->nr_parent != parent_count)
ba002f3b
MK
327 return error_func(&commit->object, FSCK_ERROR, "graft objects missing");
328 } else {
9d02150c 329 if (parent_count != parent_line_count)
ba002f3b
MK
330 return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
331 }
cf4fff57 332 if (!skip_prefix(buffer, "author ", &buffer))
ba002f3b 333 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");
daae1922
JN
334 err = fsck_ident(&buffer, &commit->object, error_func);
335 if (err)
336 return err;
cf4fff57 337 if (!skip_prefix(buffer, "committer ", &buffer))
daae1922 338 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'committer' line");
daae1922
JN
339 err = fsck_ident(&buffer, &commit->object, error_func);
340 if (err)
341 return err;
ba002f3b
MK
342 if (!commit->tree)
343 return error_func(&commit->object, FSCK_ERROR, "could not load commit's tree %s", sha1_to_hex(tree_sha1));
344
345 return 0;
346}
347
90a398bb
JS
348static int fsck_commit(struct commit *commit, const char *data,
349 unsigned long size, fsck_error error_func)
bc6b8fc1 350{
90a398bb
JS
351 const char *buffer = data ? data : get_commit_buffer(commit, &size);
352 int ret = fsck_commit_buffer(commit, buffer, size, error_func);
353 if (!data)
354 unuse_commit_buffer(commit, buffer);
bc6b8fc1
JK
355 return ret;
356}
357
90a398bb
JS
358static int fsck_tag(struct tag *tag, const char *data,
359 unsigned long size, fsck_error error_func)
ba002f3b
MK
360{
361 struct object *tagged = tag->tagged;
362
363 if (!tagged)
364 return error_func(&tag->object, FSCK_ERROR, "could not load tagged object");
365 return 0;
366}
367
90a398bb
JS
368int fsck_object(struct object *obj, void *data, unsigned long size,
369 int strict, fsck_error error_func)
ba002f3b
MK
370{
371 if (!obj)
372 return error_func(obj, FSCK_ERROR, "no valid object to fsck");
373
374 if (obj->type == OBJ_BLOB)
375 return 0;
376 if (obj->type == OBJ_TREE)
377 return fsck_tree((struct tree *) obj, strict, error_func);
378 if (obj->type == OBJ_COMMIT)
90a398bb
JS
379 return fsck_commit((struct commit *) obj, (const char *) data,
380 size, error_func);
ba002f3b 381 if (obj->type == OBJ_TAG)
90a398bb
JS
382 return fsck_tag((struct tag *) obj, (const char *) data,
383 size, error_func);
ba002f3b
MK
384
385 return error_func(obj, FSCK_ERROR, "unknown type '%d' (internal fsck error)",
386 obj->type);
387}
d6ffc8d7
MK
388
389int fsck_error_function(struct object *obj, int type, const char *fmt, ...)
390{
391 va_list ap;
f285a2d7 392 struct strbuf sb = STRBUF_INIT;
d6ffc8d7 393
5dd56489 394 strbuf_addf(&sb, "object %s:", sha1_to_hex(obj->sha1));
d6ffc8d7
MK
395
396 va_start(ap, fmt);
ebeb6090 397 strbuf_vaddf(&sb, fmt, ap);
d6ffc8d7
MK
398 va_end(ap);
399
9db56f71 400 error("%s", sb.buf);
d6ffc8d7
MK
401 strbuf_release(&sb);
402 return 1;
403}