Skip to content

Commit 1cd2913

Browse files
committed
Merge branch 'tg/maint-cache-name-compare' into maint
Even though the index can record pathnames longer than 1<<12 bytes, in some places we were not comparing them in full, potentially replacing index entries instead of adding. * tg/maint-cache-name-compare: cache_name_compare(): do not truncate while comparing paths
2 parents 1f5881d + d5f5333 commit 1cd2913

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

read-cache.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,15 @@ int df_name_compare(const char *name1, int len1, int mode1,
397397

398398
int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
399399
{
400-
int len1 = flags1 & CE_NAMEMASK;
401-
int len2 = flags2 & CE_NAMEMASK;
402-
int len = len1 < len2 ? len1 : len2;
403-
int cmp;
400+
int len1, len2, len, cmp;
401+
402+
len1 = flags1 & CE_NAMEMASK;
403+
if (CE_NAMEMASK <= len1)
404+
len1 = strlen(name1 + CE_NAMEMASK) + CE_NAMEMASK;
405+
len2 = flags2 & CE_NAMEMASK;
406+
if (CE_NAMEMASK <= len2)
407+
len2 = strlen(name2 + CE_NAMEMASK) + CE_NAMEMASK;
408+
len = len1 < len2 ? len1 : len2;
404409

405410
cmp = memcmp(name1, name2, len);
406411
if (cmp)

t/t3006-ls-files-long.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
3+
test_description='overly long paths'
4+
. ./test-lib.sh
5+
6+
test_expect_success setup '
7+
p=filefilefilefilefilefilefilefile &&
8+
p=$p$p$p$p$p$p$p$p$p$p$p$p$p$p$p$p &&
9+
p=$p$p$p$p$p$p$p$p$p$p$p$p$p$p$p$p &&
10+
11+
path_a=${p}_a &&
12+
path_z=${p}_z &&
13+
14+
blob_a=$(echo frotz | git hash-object -w --stdin) &&
15+
blob_z=$(echo nitfol | git hash-object -w --stdin) &&
16+
17+
pat="100644 %s 0\t%s\n"
18+
'
19+
20+
test_expect_success 'overly-long path by itself is not a problem' '
21+
printf "$pat" "$blob_a" "$path_a" |
22+
git update-index --add --index-info &&
23+
echo "$path_a" >expect &&
24+
git ls-files >actual &&
25+
test_cmp expect actual
26+
'
27+
28+
test_expect_success 'overly-long path does not replace another by mistake' '
29+
printf "$pat" "$blob_a" "$path_a" "$blob_z" "$path_z" |
30+
git update-index --add --index-info &&
31+
(
32+
echo "$path_a"
33+
echo "$path_z"
34+
) >expect &&
35+
git ls-files >actual &&
36+
test_cmp expect actual
37+
'
38+
39+
test_done

0 commit comments

Comments
 (0)