From c59445a88284e66bb93143991e05bb529b6ce882 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 24 Oct 2019 12:12:50 +0200 Subject: [PATCH] fixup! built-in add -p: implement the hunk splitting feature Let's be a bit more defensive in the code that finds the next line: if we are already at the end of the buffer, we definitely do _not_ want to return an offset that is outside the buffer! Signed-off-by: Johannes Schindelin --- add-patch.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/add-patch.c b/add-patch.c index fa28507810..dd1fb0d644 100644 --- a/add-patch.c +++ b/add-patch.c @@ -562,8 +562,13 @@ mismatched_output: static size_t find_next_line(struct strbuf *sb, size_t offset) { - char *eol = memchr(sb->buf + offset, '\n', sb->len - offset); + char *eol; + if (offset >= sb->len) + BUG("looking for next line beyond buffer (%d >= %d)\n%s", + (int)offset, (int)sb->len, sb->buf); + + eol = memchr(sb->buf + offset, '\n', sb->len - offset); if (!eol) return sb->len; return eol - sb->buf + 1;