find_reference_location(): make function safe for empty snapshots
authorMichael Haggerty <mhagger@alum.mit.edu>
Wed, 24 Jan 2018 11:14:13 +0000 (12:14 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jan 2018 20:55:26 +0000 (12:55 -0800)
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 <mhagger@alum.mit.edu>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs/packed-backend.c
index 8beb99652f5d79a92cc7b61359bc08ab3067bfca..fffface4283f4be94012a9f40ad0977044b5030f 100644 (file)
@@ -519,9 +519,11 @@ static int load_contents(struct snapshot *snapshot)
  * `refname` starts. If `mustexist` is true and the reference doesn't
  * exist, then return NULL. If `mustexist` is false and the reference
  * doesn't exist, then return the point where that reference would be
- * inserted. In the latter mode, `refname` doesn't have to be a proper
- * reference name; for example, one could search for "refs/replace/"
- * to find the start of any replace references.
+ * inserted, or `snapshot->eof` (which might be NULL) if it would be
+ * inserted at the end of the file. In the latter mode, `refname`
+ * doesn't have to be a proper reference name; for example, one could
+ * search for "refs/replace/" to find the start of any replace
+ * references.
  *
  * The record is sought using a binary search, so `snapshot->buf` must
  * be sorted.
@@ -551,7 +553,7 @@ static const char *find_reference_location(struct snapshot *snapshot,
         */
        const char *hi = snapshot->eof;
 
-       while (lo < hi) {
+       while (lo != hi) {
                const char *mid, *rec;
                int cmp;