summaryrefslogtreecommitdiff
path: root/usr.bin/make/parse.c
diff options
context:
space:
mode:
authorrillig <rillig@NetBSD.org>2020-12-22 08:05:08 +0000
committerrillig <rillig@NetBSD.org>2020-12-22 08:05:08 +0000
commit9e661d02a56e5753fb2ea74c0f79351cacf361ea (patch)
tree0fc649e1b678e89d97bdd5432fe9d5a4493dea54 /usr.bin/make/parse.c
parent43d2fe8635ba903eaa5f910358b6bac77192de43 (diff)
make(1): fix assertion failure for files without trailing newline
Previously, mmapped files didn't always have the final newline added. Only those that ended at a page boundary did. This confused ParseRawLine, which assumed (and since parse.c 1.510 from moments ago also asserted) that every line ends with a newline, which allows the code to assume that after a backslash, there is at least one other character in the buffer, thereby preventing an out-of-bounds read. This bug had been there at least since parse.c 1.170 from 2010-12-25 04:57:07, maybe even earlier, I didn't check. Now line_end always points to the trailing newline, which allows ParseGetLine to overwrite that character to end the string.
Diffstat (limited to 'usr.bin/make/parse.c')
-rw-r--r--usr.bin/make/parse.c47
1 files changed, 20 insertions, 27 deletions
diff --git a/usr.bin/make/parse.c b/usr.bin/make/parse.c
index db6fca0f002..c4751531743 100644
--- a/usr.bin/make/parse.c
+++ b/usr.bin/make/parse.c
@@ -1,4 +1,4 @@
-/* $NetBSD: parse.c,v 1.510 2020/12/22 06:48:33 rillig Exp $ */
+/* $NetBSD: parse.c,v 1.511 2020/12/22 08:05:08 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@@ -117,7 +117,7 @@
#include "pathnames.h"
/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: parse.c,v 1.510 2020/12/22 06:48:33 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.511 2020/12/22 08:05:08 rillig Exp $");
/* types and constants */
@@ -466,13 +466,14 @@ loadedfile_mmap(struct loadedfile *lf, int fd)
if (lf->buf == MAP_FAILED)
return FALSE;
- if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
- char *b = bmake_malloc(lf->len + 1);
- b[lf->len] = '\n';
- memcpy(b, lf->buf, lf->len++);
- munmap(lf->buf, lf->maplen);
- lf->maplen = 0;
- lf->buf = b;
+ if (lf->len > 0 && lf->buf[lf->len - 1] != '\n') {
+ if (lf->len == lf->maplen) {
+ char *b = bmake_malloc(lf->len + 1);
+ memcpy(b, lf->buf, lf->len);
+ munmap(lf->buf, lf->maplen);
+ lf->maplen = 0;
+ }
+ lf->buf[lf->len++] = '\n';
}
return TRUE;
@@ -2687,19 +2688,15 @@ ParseRawLine(IFile *curFile, char **out_line, char **out_line_end,
if (ch == '\\') {
if (firstBackslash == NULL)
firstBackslash = p;
- /*
- * FIXME: In opt-file.mk, this command succeeds:
- * printf '%s' 'V=v\' | make -r -f -
- * Using an intermediate file fails though:
- * printf '%s' 'V=v\' > backslash
- * make -r -f backslash
- *
- * In loadedfile_mmap, the trailing newline is not
- * added in every case, only if the file ends at a
- * page boundary.
- */
- if (p[1] == '\n')
+ if (p[1] == '\n') {
curFile->lineno++;
+ if (p + 2 == curFile->buf_end) {
+ line_end = p;
+ *line_end = '\n';
+ p += 2;
+ continue;
+ }
+ }
p += 2;
line_end = p;
assert(p <= curFile->buf_end);
@@ -2843,12 +2840,8 @@ ParseGetLine(GetLineMode mode)
}
/* We now have a line of data */
- /*
- * FIXME: undefined behavior since line_end points right
- * after the allocated buffer. This becomes apparent when
- * using a strict malloc implementation that adds canaries
- * before and after the allocated space.
- */
+ /* TODO: Remove line_end, it's not necessary here. */
+ assert(*line_end == '\n');
*line_end = '\0';
if (mode == GLM_FOR_BODY)