Skip to content

Commit 0eb0fb8

Browse files
rscharfegitster
authored andcommitted
sha1-lookup: handle duplicates in sha1_pos()
If the first 18 bytes of the SHA1's of all entries are the same then sha1_pos() dies and reports that the lower and upper limits of the binary search were the same that this wasn't supposed to happen. This is wrong because the remaining two bytes could still differ. Furthermore: It wouldn't be a problem if they actually were the same, i.e. if all entries have the same SHA1. The code already handles duplicates just fine. Simply remove the erroneous check. Signed-off-by: Rene Scharfe <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 38d905b commit 0eb0fb8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

sha1-lookup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ int sha1_pos(const unsigned char *sha1, void *table, size_t nr,
8484
die("BUG: assertion failed in binary search");
8585
}
8686
}
87-
if (18 <= ofs)
88-
die("cannot happen -- lo and hi are identical");
8987
}
9088

9189
do {

t/t0064-sha1-array.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,24 @@ test_expect_success 'lookup non-existing entry with duplicates' '
7171
test "$n" -lt 0
7272
'
7373

74+
test_expect_success 'lookup with almost duplicate values' '
75+
{
76+
echo "append 5555555555555555555555555555555555555555" &&
77+
echo "append 555555555555555555555555555555555555555f" &&
78+
echo20 lookup 55
79+
} | test-sha1-array >actual &&
80+
n=$(cat actual) &&
81+
test "$n" -eq 0
82+
'
83+
84+
test_expect_success 'lookup with single duplicate value' '
85+
{
86+
echo20 append 55 55 &&
87+
echo20 lookup 55
88+
} | test-sha1-array >actual &&
89+
n=$(cat actual) &&
90+
test "$n" -ge 0 &&
91+
test "$n" -le 1
92+
'
93+
7494
test_done

0 commit comments

Comments
 (0)