From 4835edfa0d452a9d75eeb73c3414d563ec9909f9 Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Mon, 9 Oct 2017 14:50:02 +0000 Subject: [PATCH] patch(1): Don't overrun line buffer in some cases Patches like file.txt attached to PR 190195 with a final line formed like ">(EOL)" could cause a copy past the end of the current line buffer. In the case of PR 191641, this caused a duplicate line to be copied into the resulting file. Instead of running past the end, treat it as if it were a blank line. PR: 191641 Reviewed by: cem, emaste, pfg Approved by: emaste (mentor) Differential Revision: https://reviews.freebsd.org/D12609 --- usr.bin/patch/pch.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index 563c539b568..d9087bb3239 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -1135,7 +1135,12 @@ hunk_done: if (*buf != '>') fatal("> expected at line %ld of patch\n", p_input_line); - p_line[i] = savestr(buf + 2); + /* Don't overrun if we don't have enough line */ + if (len > 2) + p_line[i] = savestr(buf + 2); + else + p_line[i] = savestr(""); + if (out_of_mem) { p_end = i - 1; return false;