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>
Michael Haggerty committed
Sep 13, 2017 at 19:15 UTC
735267aa100e5e75e5b8c74d47f412ad50851ec9
1 file changed
+25
-3
refs/packed-backend.c
+25
-3
@@ -161,6 +161,29 @@ static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
161
return ref;
162
}
163
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
+
187
/*
188
* Read from the `packed-refs` file into a newly-allocated
189
* `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)
250
const char *traits;
251
252
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);
254
255
if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
256
if (strstr(traits, " fully-peeled "))
@@ -266,8 +289,7 @@ static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
289
*/
290
last->flag |= REF_KNOWS_PEELED;
291
} 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);
293
}
294
}
295