make: error out if a pre-C99 platform defines bool in some cases

On NetBSD/amd64 9.99.88, when compiling make in C90 mode, make.h defined
its own boolean type as an alias for unsigned int.  Not plain int since
that would make the value of bit-fields -1 instead of 1.

This worked fine for all files except main.c, which includes
<sys/sysctl.h>, which in turn includes <stdbool.h> unconditionally, even
in C90 mode.  This meant that in main.c, sizeof(bool) was 1, while in
all other files it was 4.

This in turn led to a segmentation fault when ParseDependencySourceMain
tried to access opts.create.  Since parse.c assumed sizeof(bool) == 4,
it computed an offset outside of struct CmdOpts, which was defined in
main.c with sizeof(bool) == 1.

Rather than risking these segmentation faults, prevent building make on
platforms like these and suggest a proper workaround.
This commit is contained in:
rillig 2021-09-12 09:51:14 +00:00
parent 1d1583e21f
commit 9646fe36d1
2 changed files with 24 additions and 11 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: make.h,v 1.264 2021/07/31 09:30:17 rillig Exp $ */
/* $NetBSD: make.h,v 1.265 2021/09/12 09:51:14 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -142,18 +142,25 @@
#if __STDC_VERSION__ >= 199901L || defined(lint) || defined(USE_C99_BOOLEAN)
#include <stdbool.h>
#elif defined(__bool_true_false_are_defined)
/*
* All files of make must be compiled with the same definition of bool.
* Since one of the files includes <stdbool.h>, that means the header is
* available on this platform. Recompile everything with -DUSE_C99_BOOLEAN.
*/
#error "<stdbool.h> is included in pre-C99 mode"
#elif defined(bool) || defined(true) || defined(false)
/*
* In pre-C99 mode, make does not expect that bool is already defined.
* You need to ensure that all translation units use the same definition for
* bool.
*/
#error "bool/true/false is defined in pre-C99 mode"
#else
#ifndef bool
typedef unsigned int Boolean;
#define bool Boolean
#endif
#ifndef true
typedef unsigned char bool;
#define true 1
#endif
#ifndef false
#define false 0
#endif
#endif
#include "lst.h"
#include "enum.h"

View File

@ -1,5 +1,5 @@
#! /bin/sh
# $NetBSD: test-variants.sh,v 1.10 2021/04/03 11:08:40 rillig Exp $
# $NetBSD: test-variants.sh,v 1.11 2021/09/12 09:51:14 rillig Exp $
#
# Build several variants of make and run the tests on them.
#
@ -22,7 +22,7 @@ testcase() {
env -i PATH="$PATH" USETOOLS="no" "$@" \
sh -ce "make -s cleandir" \
&& env -i PATH="$PATH" USETOOLS="no" "$@" \
sh -ce "make -ks all" \
sh -ce "make -ks -j6 dependall" \
&& size *.o make \
&& env -i PATH="$PATH" USETOOLS="no" MALLOC_OPTIONS="JA" \
_MKMSG_TEST=":" "$@" \
@ -120,10 +120,16 @@ testcase USER_CFLAGS="-O0 -ggdb"
# Ensure that every inline function is declared as MAKE_ATTR_UNUSED.
testcase USER_CPPFLAGS="-Dinline="
# Is expected to fail with "<stdbool.h> is included in pre-C99 mode".
testcase USER_CFLAGS="-std=c90" USER_CPPFLAGS="-Dinline="
# This workaround is necessary on NetBSD 9.99 since main.c includes
# <sys/sysctl.h>, which includes <stdbool.h> even in pre-C99 mode.
testcase USER_CFLAGS="-std=c90" USER_CPPFLAGS="-Dinline= -DUSE_C99_BOOLEAN"
#testcase USER_CFLAGS="-std=c90 -pedantic" USER_CPPFLAGS="-Dinline="
# Is expected to fail with "<stdbool.h> is included in pre-C99 mode".
testcase USER_CFLAGS="-ansi" USER_CPPFLAGS="-Dinline="
# config.h does not allow overriding these features