aboutsummaryrefslogtreecommitdiff
path: root/refs
diff options
context:
space:
mode:
authorMichael Haggerty <mhagger@alum.mit.edu>2017-09-13 19:15:58 +0200
committerJunio C Hamano <gitster@pobox.com>2017-09-14 15:19:07 +0900
commit735267aa100e5e75e5b8c74d47f412ad50851ec9 (patch)
treef15def4c00d7ed783c2307beb3088034e58806a5 /refs
parentf0a7dc86d2919a2b91e46503f1df173e58977ef2 (diff)
downloadgit-735267aa100e5e75e5b8c74d47f412ad50851ec9.tar.gz
git-735267aa100e5e75e5b8c74d47f412ad50851ec9.tar.xz
die_unterminated_line(), die_invalid_line(): new functions
Extract some helper functions for reporting errors. While we're at it, prevent them from spewing unlimited output to the terminal. These functions will soon have more callers. These functions accept the problematic line as a `(ptr, len)` pair rather than a NUL-terminated string, and `die_invalid_line()` checks for an EOL itself, because these calling conventions will be convenient for future callers. (Efficiency is not a concern here because these functions are only ever called if the `packed-refs` file is corrupt.) Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'refs')
-rw-r--r--refs/packed-backend.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index a3d9210cb..5c50c223e 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -161,6 +161,29 @@ static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
return ref;
}
+static NORETURN void die_unterminated_line(const char *path,
+ const char *p, size_t len)
+{
+ if (len < 80)
+ die("unterminated line in %s: %.*s", path, (int)len, p);
+ else
+ die("unterminated line in %s: %.75s...", path, p);
+}
+
+static NORETURN void die_invalid_line(const char *path,
+ const char *p, size_t len)
+{
+ const char *eol = memchr(p, '\n', len);
+
+ if (!eol)
+ die_unterminated_line(path, p, len);
+ else if (eol - p < 80)
+ die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
+ else
+ die("unexpected line in %s: %.75s...", path, p);
+
+}
+
/*
* Read from the `packed-refs` file into a newly-allocated
* `packed_ref_cache` and return it. The return value will already
@@ -227,7 +250,7 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
const char *traits;
if (!line.len || line.buf[line.len - 1] != '\n')
- die("unterminated line in %s: %s", refs->path, line.buf);
+ die_unterminated_line(refs->path, line.buf, line.len);
if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
if (strstr(traits, " fully-peeled "))
@@ -266,8 +289,7 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
*/
last->flag |= REF_KNOWS_PEELED;
} else {
- strbuf_setlen(&line, line.len - 1);
- die("unexpected line in %s: %s", refs->path, line.buf);
+ die_invalid_line(refs->path, line.buf, line.len);
}
}