files_read_raw_ref: prevent infinite retry loops in general

Limit the number of retries to 3. That should be adequate to prevent any races, while preventing the possibility of infinite loops if the logic fails to handle any other possible error modes correctly. After the fix in the previous commit, there's no known way to trigger an infinite loop, but I did manually verify that this fixes the test in that commit even when the code change is not applied. Signed-off-by: Jeff King <peff@peff.net> Reviewed-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 6, 2016 at 12:48 UTC e8c42cb9ce6a566aad797cc6c5bc1279d608d819
1 file changed +7
refs/files-backend.c
+7
@@ -1451,6 +1451,7 @@ int read_raw_ref(const char *refname, unsigned char *sha1,
1451 int fd;
1452 int ret = -1;
1453 int save_errno;
1454 + int remaining_retries = 3;
1455
1456 *type = 0;
1457 strbuf_reset(&sb_path);
@@ -1466,8 +1467,14 @@ stat_ref:
1467 * <-> symlink) between the lstat() and reading, then
1468 * we don't want to report that as an error but rather
1469 * try again starting with the lstat().
1470 + *
1471 + * We'll keep a count of the retries, though, just to avoid
1472 + * any confusing situation sending us into an infinite loop.
1473 */
1474
1475 + if (remaining_retries-- <= 0)
1476 + goto out;
1477 +
1478 if (lstat(path, &st) < 0) {
1479 if (errno != ENOENT)
1480 goto out;