Skip to content

Commit 524c018

Browse files
pks-tgitster
authored andcommitted
config: fix segfault when parsing "core.abbrev" without repo
The "core.abbrev" config allows the user to specify the minimum length when abbreviating object hashes. Next to the values "auto" and "no", this config also accepts a concrete length that needs to be bigger or equal to the minimum length and smaller or equal to the hash algorithm's hex length. While the former condition is trivial, the latter depends on the object format used by the current repository. It is thus a variable upper boundary that may either be 40 (SHA-1) or 64 (SHA-256). This has two major downsides. First, the user that specifies this config must be aware of the object hashes that its repository use. If they want to configure the value globally, then they cannot pick any value in the range `[41, 64]` if they have any repository that uses SHA-1. If they did, Git would error out when parsing the config. Second, and more importantly, parsing "core.abbrev" crashes when outside of a Git repository because we dereference `the_hash_algo` to figure out its hex length. Starting with c8aed5e (repository: stop setting SHA1 as the default object hash, 2024-05-07) though, we stopped initializing `the_hash_algo` outside of Git repositories. Fix both of these issues by not making it an error anymore when the given length exceeds the hash length. Instead, leave the abbreviated length intact. `repo_find_unique_abbrev_r()` handles this just fine except for a performance penalty which we will fix in a subsequent commit. Reported-by: Kyle Lippincott <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9eaef58 commit 524c018

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,10 +1456,10 @@ static int git_default_core_config(const char *var, const char *value,
14561456
if (!strcasecmp(value, "auto"))
14571457
default_abbrev = -1;
14581458
else if (!git_parse_maybe_bool_text(value))
1459-
default_abbrev = the_hash_algo->hexsz;
1459+
default_abbrev = GIT_MAX_HEXSZ;
14601460
else {
14611461
int abbrev = git_config_int(var, value, ctx->kvi);
1462-
if (abbrev < minimum_abbrev || abbrev > the_hash_algo->hexsz)
1462+
if (abbrev < minimum_abbrev)
14631463
return error(_("abbrev length out of range: %d"), abbrev);
14641464
default_abbrev = abbrev;
14651465
}

t/t4202-log.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,18 @@ test_expect_success 'log.abbrevCommit configuration' '
12371237
test_cmp expect.whatchanged.full actual
12381238
'
12391239

1240+
test_expect_success '--abbrev-commit with core.abbrev=false' '
1241+
git log --no-abbrev >expect &&
1242+
git -c core.abbrev=false log --abbrev-commit >actual &&
1243+
test_cmp expect actual
1244+
'
1245+
1246+
test_expect_success '--abbrev-commit with core.abbrev=9000' '
1247+
git log --no-abbrev >expect &&
1248+
git -c core.abbrev=9000 log --abbrev-commit >actual &&
1249+
test_cmp expect actual
1250+
'
1251+
12401252
test_expect_success 'show added path under "--follow -M"' '
12411253
# This tests for a regression introduced in v1.7.2-rc0~103^2~2
12421254
test_create_repo regression &&

t/t5601-clone.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ test_expect_success 'output from clone' '
4646
test $(grep Clon output | wc -l) = 1
4747
'
4848

49+
test_expect_success 'output from clone with core.abbrev does not crash' '
50+
rm -fr dst &&
51+
echo "Cloning into ${SQ}dst${SQ}..." >expect &&
52+
git -c core.abbrev=12 clone -n "file://$(pwd)/src" dst >actual 2>&1 &&
53+
test_cmp expect actual
54+
'
55+
4956
test_expect_success 'clone does not keep pack' '
5057
5158
rm -fr dst &&

0 commit comments

Comments
 (0)