Skip to content

Commit 4a14f8d

Browse files
mhaggergitster
authored andcommitted
find_reference_location(): make function safe for empty snapshots
This function had two problems if called for an empty snapshot (i.e., `snapshot->start == snapshot->eof == NULL`): * It checked `NULL < NULL`, which is undefined by C (albeit highly unlikely to fail in the real world). * (Assuming the above comparison behaved as expected), it returned NULL when `mustexist` was false, contrary to its docstring. Change the check and fix the docstring. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 27a4184 commit 4a14f8d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

refs/packed-backend.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,11 @@ static int load_contents(struct snapshot *snapshot)
519519
* `refname` starts. If `mustexist` is true and the reference doesn't
520520
* exist, then return NULL. If `mustexist` is false and the reference
521521
* doesn't exist, then return the point where that reference would be
522-
* inserted. In the latter mode, `refname` doesn't have to be a proper
523-
* reference name; for example, one could search for "refs/replace/"
524-
* to find the start of any replace references.
522+
* inserted, or `snapshot->eof` (which might be NULL) if it would be
523+
* inserted at the end of the file. In the latter mode, `refname`
524+
* doesn't have to be a proper reference name; for example, one could
525+
* search for "refs/replace/" to find the start of any replace
526+
* references.
525527
*
526528
* The record is sought using a binary search, so `snapshot->buf` must
527529
* be sorted.
@@ -551,7 +553,7 @@ static const char *find_reference_location(struct snapshot *snapshot,
551553
*/
552554
const char *hi = snapshot->eof;
553555

554-
while (lo < hi) {
556+
while (lo != hi) {
555557
const char *mid, *rec;
556558
int cmp;
557559

0 commit comments

Comments
 (0)