On Tue, Dec 18, 2018 at 08:25:27AM +0100, Martin Ågren wrote: > If `read_repository_format()` encounters an error, `format->version` > will be -1 and all other fields of `format` will be undefined. However, > in `setup_git_directory_gently()`, we use `repo_fmt.hash_algo` > regardless of the value of `repo_fmt.version`. > > This can be observed by adding this to the end of > `read_repository_format()`: > > if (format->version == -1) > format->hash_algo = 0; /* no-one should peek at this! */ > > This causes, e.g., "git branch -m q q2 without config should succeed" in > t3200 to fail with "fatal: Failed to resolve HEAD as a valid ref." > because it has moved .git/config out of the way and is now trying to use > a bad hash algorithm. > > Check that `version` is non-negative before using `hash_algo`. > > This patch adds no tests, but do note that if we skip this patch, the > next patch would cause existing tests to fail as outlined above. > > Signed-off-by: Martin Ågren <martin.agren@xxxxxxxxx> Hmm. It looks like we never set repo_fmt.hash_algo to anything besides GIT_HASH_SHA1 anyway. I guess the existing field is really just there in preparation for us eventually respecting extensions.hashAlgorithm (or whatever it's called). Given what I said in my previous email about repos with a missing "version" field, I wondered if this patch would be breaking config like: [core] # no repositoryformatversion! [extensions] hashAlgorithm = sha256 But I'd argue that: 1. That's pretty dumb config that we shouldn't need to support. Even if we care about handling the missing version for historical repos, they wouldn't be talking sha256. 2. Arguably we should not even look at extensions.* unless we see a version >= 1. But we do process them as we parse the config file. This is mostly an oversight, I think. We have to handle them as we see them, because they may come out of order with respect to the repositoryformatversion field. But we could put them into a string_list, and then only process them after we've decided which version we have. So I think your patch is doing the right thing, and won't hurt any real cases. But (of course) there are more opportunities to clean things up. -Peff