@matheusbsbs / matheus-bagliano / commits / 8dcf39c46e
Fork 0

Prevent bogus paths from being added to the index.

With this one, it's now a fatal error to try to add a pathname that cannot be added with "git add", i.e. [torvalds@g5 git]$ git add .git/config fatal: unable to add .git/config to index and [torvalds@g5 git]$ git add foo/../bar fatal: unable to add foo/../bar to index instead of the old "Ignoring path xyz" warning that would end up silently succeeding on any other paths. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 18, 2006 at 12:07 UTC 8dcf39c46e2931ca02b18b1ea3a6b21f446d8de8
3 files changed +67 -64
cache.h
+1
@@ -142,6 +142,7 @@ extern void verify_non_filename(const char *prefix, const char *name);
142 /* Initialize and use the cache information */
143 extern int read_cache(void);
144 extern int write_cache(int newfd, struct cache_entry **cache, int entries);
145 +extern int verify_path(const char *path);
146 extern int cache_name_pos(const char *name, int namelen);
147 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
148 #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
read-cache.c
+66
@@ -331,6 +331,70 @@ int ce_path_match(const struct cache_entry *ce, const char **pathspec)
331 return 0;
332 }
333
334 +/*
335 + * We fundamentally don't like some paths: we don't want
336 + * dot or dot-dot anywhere, and for obvious reasons don't
337 + * want to recurse into ".git" either.
338 + *
339 + * Also, we don't want double slashes or slashes at the
340 + * end that can make pathnames ambiguous.
341 + */
342 +static int verify_dotfile(const char *rest)
343 +{
344 + /*
345 + * The first character was '.', but that
346 + * has already been discarded, we now test
347 + * the rest.
348 + */
349 + switch (*rest) {
350 + /* "." is not allowed */
351 + case '\0': case '/':
352 + return 0;
353 +
354 + /*
355 + * ".git" followed by NUL or slash is bad. This
356 + * shares the path end test with the ".." case.
357 + */
358 + case 'g':
359 + if (rest[1] != 'i')
360 + break;
361 + if (rest[2] != 't')
362 + break;
363 + rest += 2;
364 + /* fallthrough */
365 + case '.':
366 + if (rest[1] == '\0' || rest[1] == '/')
367 + return 0;
368 + }
369 + return 1;
370 +}
371 +
372 +int verify_path(const char *path)
373 +{
374 + char c;
375 +
376 + goto inside;
377 + for (;;) {
378 + if (!c)
379 + return 1;
380 + if (c == '/') {
381 +inside:
382 + c = *path++;
383 + switch (c) {
384 + default:
385 + continue;
386 + case '/': case '\0':
387 + break;
388 + case '.':
389 + if (verify_dotfile(path))
390 + continue;
391 + }
392 + return 0;
393 + }
394 + c = *path++;
395 + }
396 +}
397 +
398 /*
399 * Do we have another file that has the beginning components being a
400 * proper superset of the name we're trying to add?
@@ -472,6 +536,8 @@ int add_cache_entry(struct cache_entry *ce, int option)
536
537 if (!ok_to_add)
538 return -1;
539 + if (!verify_path(ce->name))
540 + return -1;
541
542 if (!skip_df_check &&
543 check_file_directory_conflict(ce, pos, ok_to_replace)) {
update-index.c
-64
@@ -237,70 +237,6 @@ static int refresh_cache(int really)
237 return has_errors;
238 }
239
240 -/*
241 - * We fundamentally don't like some paths: we don't want
242 - * dot or dot-dot anywhere, and for obvious reasons don't
243 - * want to recurse into ".git" either.
244 - *
245 - * Also, we don't want double slashes or slashes at the
246 - * end that can make pathnames ambiguous.
247 - */
248 -static int verify_dotfile(const char *rest)
249 -{
250 - /*
251 - * The first character was '.', but that
252 - * has already been discarded, we now test
253 - * the rest.
254 - */
255 - switch (*rest) {
256 - /* "." is not allowed */
257 - case '\0': case '/':
258 - return 0;
259 -
260 - /*
261 - * ".git" followed by NUL or slash is bad. This
262 - * shares the path end test with the ".." case.
263 - */
264 - case 'g':
265 - if (rest[1] != 'i')
266 - break;
267 - if (rest[2] != 't')
268 - break;
269 - rest += 2;
270 - /* fallthrough */
271 - case '.':
272 - if (rest[1] == '\0' || rest[1] == '/')
273 - return 0;
274 - }
275 - return 1;
276 -}
277 -
278 -static int verify_path(const char *path)
279 -{
280 - char c;
281 -
282 - goto inside;
283 - for (;;) {
284 - if (!c)
285 - return 1;
286 - if (c == '/') {
287 -inside:
288 - c = *path++;
289 - switch (c) {
290 - default:
291 - continue;
292 - case '/': case '\0':
293 - break;
294 - case '.':
295 - if (verify_dotfile(path))
296 - continue;
297 - }
298 - return 0;
299 - }
300 - c = *path++;
301 - }
302 -}
303 -
240 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
241 const char *path, int stage)
242 {