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
This commit is contained in:
kevans 2017-10-09 14:50:02 +00:00
parent 6de51f94f8
commit 8d897cba91

View File

@ -1135,7 +1135,12 @@ another_hunk(void)
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;