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.
This commit is contained in:
rillig 2022-01-07 08:20:00 +00:00
parent 0519d6fc29
commit 03e107a990
2 changed files with 15 additions and 4 deletions

View File

@ -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);

View File

@ -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: