From 6d1faa1db35439b267a7b10725ab0876f027ecb6 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sat, 27 Feb 2021 21:27:04 -0500 Subject: [PATCH] git_refs: fix crash with binary . files in .git/refs/ On macOS, the Finder app likes to poop .DS_Store files in every path that the user browses. If the user pokes around the .git/ tree, it could generate a .DS_Store file in there too. When repo goes to read all the local refs, it tries to decode this binary file as UTF-8 and subsequently crashes. Since paths that begin with . are not valid refs, ignore them like we already do with paths that end in .lock. Also bump the check up to ignore dirs that match since that follows the git rules: they apply to any component in its path, not just the final path (name). We don't implement the full valid ref algorithm that git employs as it's a bit complicated, and we only really need to focus on what will practically show up locally. Bug: https://crbug.com/gerrit/14162 Change-Id: I6519f990e33cc58a72fcb00c0f983ad3285ace3d Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/298662 Tested-by: Mike Frysinger Reviewed-by: Michael Mortensen --- git_refs.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/git_refs.py b/git_refs.py index 3c8a9867..2d4a8090 100644 --- a/git_refs.py +++ b/git_refs.py @@ -131,11 +131,14 @@ class GitRefs(object): base = os.path.join(self._gitdir, prefix) for name in platform_utils.listdir(base): p = os.path.join(base, name) - if platform_utils.isdir(p): + # We don't implement the full ref validation algorithm, just the simple + # rules that would show up in local filesystems. + # https://git-scm.com/docs/git-check-ref-format + if name.startswith('.') or name.endswith('.lock'): + pass + elif platform_utils.isdir(p): self._mtime[prefix] = os.path.getmtime(base) self._ReadLoose(prefix + name + '/') - elif name.endswith('.lock'): - pass else: self._ReadLoose1(p, prefix + name) @@ -144,7 +147,7 @@ class GitRefs(object): with open(path) as fd: mtime = os.path.getmtime(path) ref_id = fd.readline() - except (IOError, OSError): + except (OSError, UnicodeError): return try: