Skip to content

Commit 735267a

Browse files
mhaggergitster
authored andcommitted
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>
1 parent f0a7dc8 commit 735267a

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

refs/packed-backend.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,29 @@ static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
161161
return ref;
162162
}
163163

164+
static NORETURN void die_unterminated_line(const char *path,
165+
const char *p, size_t len)
166+
{
167+
if (len < 80)
168+
die("unterminated line in %s: %.*s", path, (int)len, p);
169+
else
170+
die("unterminated line in %s: %.75s...", path, p);
171+
}
172+
173+
static NORETURN void die_invalid_line(const char *path,
174+
const char *p, size_t len)
175+
{
176+
const char *eol = memchr(p, '\n', len);
177+
178+
if (!eol)
179+
die_unterminated_line(path, p, len);
180+
else if (eol - p < 80)
181+
die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
182+
else
183+
die("unexpected line in %s: %.75s...", path, p);
184+
185+
}
186+
164187
/*
165188
* Read from the `packed-refs` file into a newly-allocated
166189
* `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)
227250
const char *traits;
228251

229252
if (!line.len || line.buf[line.len - 1] != '\n')
230-
die("unterminated line in %s: %s", refs->path, line.buf);
253+
die_unterminated_line(refs->path, line.buf, line.len);
231254

232255
if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
233256
if (strstr(traits, " fully-peeled "))
@@ -266,8 +289,7 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
266289
*/
267290
last->flag |= REF_KNOWS_PEELED;
268291
} else {
269-
strbuf_setlen(&line, line.len - 1);
270-
die("unexpected line in %s: %s", refs->path, line.buf);
292+
die_invalid_line(refs->path, line.buf, line.len);
271293
}
272294
}
273295

0 commit comments

Comments
 (0)