Require sizeof(bool) == 1.
authorThomas Munro <tmunro@postgresql.org>
Wed, 27 Nov 2024 22:48:07 +0000 (11:48 +1300)
committerThomas Munro <tmunro@postgresql.org>
Wed, 27 Nov 2024 23:01:14 +0000 (12:01 +1300)
The C standard says that sizeof(bool) is implementation-defined, but we
know of no current systems where it is not 1.  The last known systems
seem to have been Apple macOS/PowerPC 10.5 and Microsoft Visual C++ 4,
both long defunct.

PostgreSQL has always required sizeof(bool) == 1 for the definition of
bool that it used, but previously it would define its own type if the
system-provided bool had a different size.  That was liable to cause
memory layout problems when interacting with system and third-party
libraries on (by now hypothetical) computers with wider _Bool, and now
C23 has introduced a new problem by making bool a built-in datatype
(like C++), so the fallback code doesn't even compile.  We could
probably work around that, but then we'd be writing new untested code
for a computer that doesn't exist.

Instead, delete the unreachable and C23-uncompilable fallback code, and
let existing static assertions fail if the system-provided bool is too
wide.  If we ever get a problem report from a real system, then it will
be time to figure out what to do about it in a way that also works on
modern compilers.

Note on C++: Previously we avoided including <stdbool.h> or trying to
define a new bool type in headers that might be included by C++ code.
These days we might as well just include <stdbool.h> unconditionally:
it should be visible to C++11 but do nothing, just as in C23.  We
already include <stdint.h> without C++ guards in c.h, and that falls
under the same C99-compatibility section of the C++11 standard as
<stdbool.h>, so let's remove the guards here too.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/3198438.1731895163%40sss.pgh.pa.us

configure
configure.ac
meson.build
src/backend/jit/llvm/llvmjit_types.c
src/backend/utils/fmgr/dfmgr.c
src/include/c.h
src/include/pg_config.h.in
src/interfaces/ecpg/include/ecpg_config.h.in
src/interfaces/ecpg/include/ecpglib.h
src/interfaces/ecpg/include/meson.build
src/pl/plperl/plperl_system.h

index 02b2c2e10612501b8227721d743e707a776c2479..ff59f1422d8114db7afcfe61ca370b0ee159b836 100755 (executable)
--- a/configure
+++ b/configure
@@ -14958,47 +14958,6 @@ if test "$ac_cv_sizeof_off_t" -lt 8; then
   fi
 fi
 
-# The cast to long int works around a bug in the HP C Compiler
-# version HP92453-01 B.11.11.23709.GP, which incorrectly rejects
-# declarations like `int a3[[(sizeof (unsigned char)) >= 0]];'.
-# This bug is HP SR number 8606223364.
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking size of bool" >&5
-$as_echo_n "checking size of bool... " >&6; }
-if ${ac_cv_sizeof_bool+:} false; then :
-  $as_echo_n "(cached) " >&6
-else
-  if ac_fn_c_compute_int "$LINENO" "(long int) (sizeof (bool))" "ac_cv_sizeof_bool"        "#include <stdbool.h>
-"; then :
-
-else
-  if test "$ac_cv_type_bool" = yes; then
-     { { $as_echo "$as_me:${as_lineno-$LINENO}: error: in \`$ac_pwd':" >&5
-$as_echo "$as_me: error: in \`$ac_pwd':" >&2;}
-as_fn_error 77 "cannot compute sizeof (bool)
-See \`config.log' for more details" "$LINENO" 5; }
-   else
-     ac_cv_sizeof_bool=0
-   fi
-fi
-
-fi
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_sizeof_bool" >&5
-$as_echo "$ac_cv_sizeof_bool" >&6; }
-
-
-
-cat >>confdefs.h <<_ACEOF
-#define SIZEOF_BOOL $ac_cv_sizeof_bool
-_ACEOF
-
-
-
-if test "$ac_cv_sizeof_bool" = 1; then
-
-$as_echo "#define PG_USE_STDBOOL 1" >>confdefs.h
-
-fi
-
 
 ##
 ## Functions, global variables
index 99a5b1aac64b6f8349eb4bf27958f2f5d27980c3..21817009642eae3bbfd0c6e27c6c1bd6a8772405 100644 (file)
@@ -1691,15 +1691,6 @@ if test "$ac_cv_sizeof_off_t" -lt 8; then
   fi
 fi
 
-AC_CHECK_SIZEOF([bool], [], [#include <stdbool.h>])
-
-dnl We use <stdbool.h> if bool has size 1 after including it.  Otherwise, c.h
-dnl will fall back to declaring bool as unsigned char.
-if test "$ac_cv_sizeof_bool" = 1; then
-  AC_DEFINE([PG_USE_STDBOOL], 1,
-            [Define to 1 to use <stdbool.h> to define type bool.])
-fi
-
 
 ##
 ## Functions, global variables
index 6bc3bb51dc0e79f006ff1767a314515d0c6258f4..451c3f6d851d1b915ecf9eded5aeb0ed62e8f1dc 100644 (file)
@@ -1755,13 +1755,6 @@ if cc.compiles('''
 endif
 
 
-# We use <stdbool.h> if bool has size 1 after including it.  Otherwise, c.h
-# will fall back to declaring bool as unsigned char.
-if cc.sizeof('bool', prefix: '#include <stdbool.h>', args: test_c_args) == 1
-  cdata.set('PG_USE_STDBOOL', 1)
-endif
-
-
 # Need to check a call with %m because netbsd supports gnu_printf but emits a
 # warning for each use of %m.
 printf_attributes = ['gnu_printf', '__syslog__', 'printf']
index 4a9e07701458b05ecf52dba10f0ed3394d1cdd43..13efe18e6b08f9716ea1ed108c9bee17d50b300e 100644 (file)
@@ -117,11 +117,9 @@ ExecEvalBoolSubroutineTemplate(ExprState *state,
 }
 
 /*
- * Clang represents stdbool.h style booleans that are returned by functions
- * differently (as i1) than stored ones (as i8). Therefore we do not just need
- * TypeBool (above), but also a way to determine the width of a returned
- * integer. This allows us to keep compatible with non-stdbool using
- * architectures.
+ * Clang represents bool returned by functions differently (as i1) than stored
+ * ones (as i8).  Therefore we do not just need TypeStorageBool (above), but
+ * also a way to determine the width of a returned integer.
  */
 extern bool FunctionReturningBool(void);
 bool
index c7aa789b51b41088fe68dcc66036d73f731e49da..8e81ecc7491e8b2cdb5aed9977fee824ad7b6404 100644 (file)
 
 #ifndef WIN32
 #include <dlfcn.h>
-
-/*
- * On macOS, <dlfcn.h> insists on including <stdbool.h>.  If we're not
- * using stdbool, undef bool to undo the damage.
- */
-#ifndef PG_USE_STDBOOL
-#ifdef bool
-#undef bool
-#endif
-#endif
 #endif                                                 /* !WIN32 */
 
 #include "fmgr.h"
index fe1ce87882be48d0f974615a08b861de3f6ac911..304dff27e34aeee78dded3bc3404cc3350146951 100644 (file)
@@ -450,37 +450,11 @@ typedef void (*pg_funcptr_t) (void);
  * bool
  *             Boolean value, either true or false.
  *
- * We use stdbool.h if bool has size 1 after including it.  That's useful for
- * better compiler and debugger output and for compatibility with third-party
- * libraries.  But PostgreSQL currently cannot deal with bool of other sizes;
- * there are static assertions around the code to prevent that.
- *
- * For C++ compilers, we assume the compiler has a compatible built-in
- * definition of bool.
- *
- * See also the version of this code in src/interfaces/ecpg/include/ecpglib.h.
+ * PostgreSQL currently cannot deal with bool of size other than 1; there are
+ * static assertions around the code to prevent that.
  */
 
-#ifndef __cplusplus
-
-#ifdef PG_USE_STDBOOL
 #include <stdbool.h>
-#else
-
-#ifndef bool
-typedef unsigned char bool;
-#endif
-
-#ifndef true
-#define true   ((bool) 1)
-#endif
-
-#ifndef false
-#define false  ((bool) 0)
-#endif
-
-#endif                                                 /* not PG_USE_STDBOOL */
-#endif                                                 /* not C++ */
 
 
 /* ----------------------------------------------------------------
index 6e25afc58fd69bdef10d8e1fd16010f3b56bf43f..ab0f8cc2b4a0de0922aa037471103f0f10d35272 100644 (file)
 /* Define to best printf format archetype, usually gnu_printf if available. */
 #undef PG_PRINTF_ATTRIBUTE
 
-/* Define to 1 to use <stdbool.h> to define type bool. */
-#undef PG_USE_STDBOOL
-
 /* PostgreSQL version as a string */
 #undef PG_VERSION
 
    RELSEG_SIZE requires an initdb. */
 #undef RELSEG_SIZE
 
-/* The size of `bool', as computed by sizeof. */
-#undef SIZEOF_BOOL
-
 /* The size of `long', as computed by sizeof. */
 #undef SIZEOF_LONG
 
index 2b439c1e58d7136ca84f96ca6d58f4f2def17925..75f542f263b6e200f091170c50c8b04efa250ef5 100644 (file)
@@ -6,6 +6,3 @@
 
 /* Define to 1 if `long long int' works and is 64 bits. */
 #undef HAVE_LONG_LONG_INT_64
-
-/* Define to 1 to use <stdbool.h> to define type bool. */
-#undef PG_USE_STDBOOL
index 771761ffe4969f9262fd0be40f7587326af30f94..dbf3cc3a0c26548d0667eb08acbf416290b283e8 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef _ECPGLIB_H
 #define _ECPGLIB_H
 
+#include <stdbool.h>
 #include <string.h>
 
 #include "ecpg_config.h"
 #include "libpq-fe.h"
 #include "sqlca.h"
 
-/*
- * This is a small extract from c.h since we don't want to leak all postgres
- * definitions into ecpg programs; but we need to know what bool is.
- */
-#ifndef __cplusplus
-
-#ifdef PG_USE_STDBOOL
-#include <stdbool.h>
-#else
-
-/*
- * We assume bool has been defined if true and false are.  This avoids
- * duplicate-typedef errors if this file is included after c.h.
- */
-#if !(defined(true) && defined(false))
-typedef unsigned char bool;
-#endif
-
-#ifndef true
-#define true   ((bool) 1)
-#endif
-
-#ifndef false
-#define false  ((bool) 0)
-#endif
-
-#endif                                                 /* not PG_USE_STDBOOL */
-#endif                                                 /* not C++ */
-
 
 #ifdef __cplusplus
 extern "C"
index b4a5aedd51fe802aa24fb205ad701699868f4411..a3beb3cc7be3d2e076ea79435d05f226a9eeef34 100644 (file)
@@ -5,7 +5,6 @@ ecpg_inc = include_directories('.')
 ecpg_conf_keys = [
   'HAVE_LONG_INT_64',
   'HAVE_LONG_LONG_INT_64',
-  'PG_USE_STDBOOL',
 ]
 
 ecpg_conf_data = configuration_data()
index 48f6575e513b79dd6d838f4f6ea66d93d3a907d2..d1b9ea17fca2e864c302741b9e72bc124fd81eaf 100644 (file)
 #endif
 
 /*
- * Regarding bool, both PostgreSQL and Perl might use stdbool.h or not,
- * depending on configuration.  If both agree, things are relatively harmless.
- * If not, things get tricky.  If PostgreSQL does but Perl does not, define
- * HAS_BOOL here so that Perl does not redefine bool; this avoids compiler
- * warnings.  If PostgreSQL does not but Perl does, we need to undefine bool
- * after we include the Perl headers; see below.
+ * Define HAS_BOOL here so that Perl does not redefine bool.  We included
+ * <stdbool.h> in c.h.
  */
-#ifdef PG_USE_STDBOOL
 #define HAS_BOOL 1
-#endif
 
 /*
  * Get the basic Perl API.  We use PERL_NO_GET_CONTEXT mode so that our code
 /* perl version and platform portability */
 #include "ppport.h"
 
-/*
- * perl might have included stdbool.h.  If we also did that earlier (see c.h),
- * then that's fine.  If not, we probably rejected it for some reason.  In
- * that case, undef bool and proceed with our own bool.  (Note that stdbool.h
- * makes bool a macro, but our own replacement is a typedef, so the undef
- * makes ours visible again).
- */
-#ifndef PG_USE_STDBOOL
-#ifdef bool
-#undef bool
-#endif
-#endif
-
 /* supply HeUTF8 if it's missing - ppport.h doesn't supply it, unfortunately */
 #ifndef HeUTF8
 #define HeUTF8(he)                        ((HeKLEN(he) == HEf_SVKEY) ?                    \