Module Name:    src
Committed By:   rillig
Date:           Fri Jan  7 08:20:00 UTC 2022

Modified Files:
        src/usr.bin/make: parse.c
        src/usr.bin/make/unit-tests: directive-include.mk

Log Message:
make: fix null pointer when including empty file (since 2022-01-01)

Calling malloc(0) may return a null pointer, but callers of bmake_malloc
do not expect that.

Reported by Chris Pinnock, found by cross-compiling NetBSD on OpenBSD,
where tools/groff creates Makefile.dep files of size 0.


To generate a diff of this commit:
cvs rdiff -u -r1.619 -r1.620 src/usr.bin/make/parse.c
cvs rdiff -u -r1.9 -r1.10 src/usr.bin/make/unit-tests/directive-include.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.619 src/usr.bin/make/parse.c:1.620
--- src/usr.bin/make/parse.c:1.619	Sun Jan  2 02:57:39 2022
+++ src/usr.bin/make/parse.c	Fri Jan  7 08:20:00 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.619 2022/01/02 02:57:39 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.620 2022/01/07 08:20:00 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -106,7 +106,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.619 2022/01/02 02:57:39 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.620 2022/01/07 08:20:00 rillig Exp $");
 
 /*
  * Structure for a file being read ("included file")
@@ -297,7 +297,7 @@ loadfile(const char *path, int fd)
 	struct stat st;
 
 	bufSize = fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
-		  st.st_size >= 0 && st.st_size <= 0x3fffffff
+		  st.st_size >= 1 && st.st_size <= 0x3fffffff
 	    ? (size_t)st.st_size : 1024;
 	Buf_InitSize(&buf, bufSize);
 

Index: src/usr.bin/make/unit-tests/directive-include.mk
diff -u src/usr.bin/make/unit-tests/directive-include.mk:1.9 src/usr.bin/make/unit-tests/directive-include.mk:1.10
--- src/usr.bin/make/unit-tests/directive-include.mk:1.9	Tue Dec 14 01:00:04 2021
+++ src/usr.bin/make/unit-tests/directive-include.mk	Fri Jan  7 08:20:00 2022
@@ -1,4 +1,4 @@
-# $NetBSD: directive-include.mk,v 1.9 2021/12/14 01:00:04 rillig Exp $
+# $NetBSD: directive-include.mk,v 1.10 2022/01/07 08:20:00 rillig Exp $
 #
 # Tests for the .include directive, which includes another file.
 
@@ -71,4 +71,15 @@ include
 # or any other indicator for the empty filename at the end of the line.
 #include ${:U}
 
+
+# Since parse.c 1.612 from 2022-01-01 and before parse.c 1.620 from
+# 2022-01-07, including an empty regular file called bmake_malloc(0), which
+# may return a null pointer.  On OpenBSD, this led to a segmentation fault in
+# Buf_InitSize, which assumes that bmake_malloc never returns NULL, just like
+# all other places in the code.
+_!=		> directive-include-empty
+.include "${.CURDIR}/directive-include-empty"
+_!=		rm directive-include-empty
+
+
 all:

Reply via email to