Module Name:    src
Committed By:   rillig
Date:           Sun Dec  6 20:33:44 UTC 2020

Modified Files:
        src/usr.bin/make: parse.c
        src/usr.bin/make/unit-tests: opt-file.exp opt-file.mk

Log Message:
make(1): error out on null bytes in makefiles

Makefiles are text files, they must not contain null bytes.

The previous code in this area was rotten anyway.  It assumed that
buf_end could be NULL even if buf_ptr was a valid pointer, which is no
longer true, probably since a few years already.

Continuing parsing after a null byte does not make sense.  If there's a
null byte in a text file, that file is corrupted, and parsing it leads
to unintended effects easily.  Therefore the only sensible action is to
stop parsing immediately.

The check whether cf->readMore could be null was outdated as well, which
previously made the fatal error impossible to reach.  Because of the
missing unit tests, nobody noticed this though.

The "exit status 0" in opt-file.exp is worring but that's due to another
bug and will be fixed in a follow-up commit.


To generate a diff of this commit:
cvs rdiff -u -r1.471 -r1.472 src/usr.bin/make/parse.c
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/make/unit-tests/opt-file.exp
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/make/unit-tests/opt-file.mk

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.471 src/usr.bin/make/parse.c:1.472
--- src/usr.bin/make/parse.c:1.471	Sun Dec  6 20:09:01 2020
+++ src/usr.bin/make/parse.c	Sun Dec  6 20:33:44 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.471 2020/12/06 20:09:01 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.472 2020/12/06 20:33:44 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.471 2020/12/06 20:09:01 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.472 2020/12/06 20:33:44 rillig Exp $");
 
 /* types and constants */
 
@@ -2687,41 +2687,15 @@ ParseGetLine(GetLineMode mode)
 		escaped = NULL;
 		comment = NULL;
 		for (;;) {
-			/* XXX: can buf_end ever be null? */
-			if (cf->buf_end != NULL && ptr == cf->buf_end) {
+			if (ptr == cf->buf_end) {
 				/* end of buffer */
 				ch = '\0';
 				break;
 			}
 			ch = *ptr;
-			if (ch == '\0' || (ch == '\\' && ptr[1] == '\0')) {
-
-				/* XXX: can buf_end ever be null? */
-				if (cf->buf_end == NULL)
-					/* End of string (aka for loop) data */
-					break;
-
-				/* see if there is more we can parse */
-				while (ptr++ < cf->buf_end) {
-					if ((ch = *ptr) == '\n') {
-						if (ptr > line &&
-						    ptr[-1] == '\\')
-							continue;
-						Parse_Error(PARSE_WARNING,
-						    "Zero byte read from file, "
-						    "skipping rest of line.");
-						break;
-					}
-				}
-				/* XXX: Can cf->readMore ever be NULL? */
-				if (cf->readMore != NULL) {
-					/*
-					 * End of this buffer; return EOF and
-					 * outer logic will get the next one.
-					 * (eww)
-					 */
-					break;
-				}
+			if (ch == '\0' ||
+			    (ch == '\\' && ptr + 1 < cf->buf_end &&
+			     ptr[1] == '\0')) {
 				Parse_Error(PARSE_FATAL,
 				    "Zero byte read from file");
 				return NULL;

Index: src/usr.bin/make/unit-tests/opt-file.exp
diff -u src/usr.bin/make/unit-tests/opt-file.exp:1.3 src/usr.bin/make/unit-tests/opt-file.exp:1.4
--- src/usr.bin/make/unit-tests/opt-file.exp:1.3	Sun Dec  6 19:18:26 2020
+++ src/usr.bin/make/unit-tests/opt-file.exp	Sun Dec  6 20:33:44 2020
@@ -1,5 +1,7 @@
 value
-make: "(stdin)" line 1: warning: Zero byte read from file, skipping rest of line.
-va
-VALUE2
+make: "(stdin)" line 1: Zero byte read from file
+make: Fatal errors encountered -- cannot continue
+make: stopped in unit-tests
+*** Error code 1 (continuing)
+`all' not remade because of errors.
 exit status 0

Index: src/usr.bin/make/unit-tests/opt-file.mk
diff -u src/usr.bin/make/unit-tests/opt-file.mk:1.5 src/usr.bin/make/unit-tests/opt-file.mk:1.6
--- src/usr.bin/make/unit-tests/opt-file.mk:1.5	Sun Dec  6 20:07:25 2020
+++ src/usr.bin/make/unit-tests/opt-file.mk	Sun Dec  6 20:33:44 2020
@@ -1,4 +1,4 @@
-# $NetBSD: opt-file.mk,v 1.5 2020/12/06 20:07:25 rillig Exp $
+# $NetBSD: opt-file.mk,v 1.6 2020/12/06 20:33:44 rillig Exp $
 #
 # Tests for the -f command line option.
 
@@ -49,15 +49,19 @@ file-ending-in-backslash: .PHONY
 #	make: stopped in .
 #	exit status 2
 #
-#	2014:
+#	2014 to 2020-12-06:
 #	make: "zero-byte.in" line 1: warning: Zero byte read from file, skipping rest of line.
 #	exit status 0
 #
-# XXX: It would be safer to just quit parsing in such a situation.
+#	Since 2020-12-07:
+#	make: "zero-byte.in" line 1: Zero byte read from file
+#	make: Fatal errors encountered -- cannot continue
+#	make: stopped in .
+#	exit status 1
 file-containing-null-byte: .PHONY
 	@printf '%s\n' 'VAR=value' 'VAR2=VALUE2' \
 	| tr 'l' '\0' \
 	| ${MAKE} -r -f - -v VAR -v VAR2
 
 all:
-	@:;
+	: Making ${.TARGET}

Reply via email to