Skip to content

Commit 0993019

Browse files
committed
sha1_position: convert do-while to while
If we enter the sha1_position() function with "lo == hi", we have no elements. But the do-while loop means that we'll enter the loop body once anyway, picking "mi" at that same value and comparing nonsense to our desired key. This is unlikely to match in practice, but we still shouldn't be looking at the memory in the first place. This bug is inherited from git.git; it was fixed there in e01580cfe01526ec2c4eb4899f776a82ade7e0e1.
1 parent a9d6b9d commit 0993019

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/sha1_lookup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ int sha1_position(const void *table,
232232
{
233233
const unsigned char *base = table;
234234

235-
do {
235+
while (lo < hi) {
236236
unsigned mi = (lo + hi) / 2;
237237
int cmp = git_oid__hashcmp(base + mi * stride, key);
238238

@@ -243,7 +243,7 @@ int sha1_position(const void *table,
243243
hi = mi;
244244
else
245245
lo = mi+1;
246-
} while (lo < hi);
246+
}
247247

248248
return -((int)lo)-1;
249249
}

0 commit comments

Comments
 (0)