Skip to content

Commit 9a95a77

Browse files
committed
Use stdbool.h if suitable
Using the standard bool type provided by C allows some recent compilers and debuggers to give better diagnostics. Also, some extension code and third-party headers are increasingly pulling in stdbool.h, so it's probably saner if everyone uses the same definition. But PostgreSQL code is not prepared to handle bool of a size other than 1, so we keep our own old definition if we encounter a stdbool.h with a bool of a different size. (Among current build farm members, this only applies to old macOS versions on PowerPC.) To check that the used bool is of the right size, add a static assertions about size of GinTernaryValue vs bool. This is currently the only place that assumes that bool and char are of the same size. Discussion: https://www.postgresql.org/message-id/flat/3a0fe7e1-5ed1-414b-9230-53bbc0ed1f49@2ndquadrant.com
1 parent 2a0faed commit 9a95a77

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/backend/utils/adt/tsginidx.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ gin_tsquery_consistent(PG_FUNCTION_ARGS)
309309
* query.
310310
*/
311311
gcv.first_item = GETQUERY(query);
312-
gcv.check = check;
312+
StaticAssertStmt(sizeof(GinTernaryValue) == sizeof(bool),
313+
"sizes of GinTernaryValue and bool are not equal");
314+
gcv.check = (GinTernaryValue *) check;
313315
gcv.map_item_operand = (int *) (extra_data[0]);
314316
gcv.need_recheck = recheck;
315317

src/include/access/gin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ typedef struct GinStatsData
5151
/*
5252
* A ternary value used by tri-consistent functions.
5353
*
54-
* For convenience, this is compatible with booleans. A boolean can be
55-
* safely cast to a GinTernaryValue.
54+
* This must be of the same size as a bool because some code will cast a
55+
* pointer to a bool to a pointer to a GinTernaryValue.
5656
*/
5757
typedef char GinTernaryValue;
5858

src/include/c.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,21 @@
255255
* bool
256256
* Boolean value, either true or false.
257257
*
258-
* XXX for C++ compilers, we assume the compiler has a compatible
259-
* built-in definition of bool.
258+
* Use stdbool.h if available and its bool has size 1. That's useful for
259+
* better compiler and debugger output and for compatibility with third-party
260+
* libraries. But PostgreSQL currently cannot deal with bool of other sizes;
261+
* there are static assertions around the code to prevent that.
262+
*
263+
* For C++ compilers, we assume the compiler has a compatible built-in
264+
* definition of bool.
260265
*/
261266

262267
#ifndef __cplusplus
263268

269+
#if defined(HAVE_STDBOOL_H) && SIZEOF_BOOL == 1
270+
#include <stdbool.h>
271+
#else
272+
264273
#ifndef bool
265274
typedef char bool;
266275
#endif
@@ -273,6 +282,7 @@ typedef char bool;
273282
#define false ((bool) 0)
274283
#endif
275284

285+
#endif
276286
#endif /* not C++ */
277287

278288

src/include/pg_config.h.win32

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@
356356
/* Define to 1 if you have the `SSL_get_current_compression' function. */
357357
#define HAVE_SSL_GET_CURRENT_COMPRESSION 1
358358

359+
/* Define to 1 if stdbool.h conforms to C99. */
360+
/* #undef HAVE_STDBOOL_H */
361+
359362
/* Define to 1 if you have the <stdint.h> header file. */
360363
/* #undef HAVE_STDINT_H */
361364

@@ -524,6 +527,9 @@
524527
/* Define to 1 if you have the <winldap.h> header file. */
525528
/* #undef HAVE_WINLDAP_H */
526529

530+
/* Define to 1 if the system has the type `_Bool'. */
531+
/* #undef HAVE__BOOL */
532+
527533
/* Define to 1 if your compiler understands __builtin_bswap16. */
528534
/* #undef HAVE__BUILTIN_BSWAP16 */
529535

@@ -606,6 +612,9 @@
606612
/* A string containing the version number, platform, and C compiler */
607613
#define PG_VERSION_STR "Uninitialized version string (win32)"
608614

615+
/* The size of `bool', as computed by sizeof. */
616+
#define SIZEOF_BOOL 0
617+
609618
/* The size of `long', as computed by sizeof. */
610619
#define SIZEOF_LONG 4
611620

src/pl/plperl/plperl.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
#define __inline__ inline
5151
#endif
5252

53+
/*
54+
* Prevent perl from redefining "bool".
55+
*/
56+
#define HAS_BOOL 1
57+
5358

5459
/*
5560
* Get the basic Perl API. We use PERL_NO_GET_CONTEXT mode so that our code
@@ -91,11 +96,6 @@
9196
#define NEED_sv_2pv_flags
9297
#include "ppport.h"
9398

94-
/* perl may have a different width of "bool", don't buy it */
95-
#ifdef bool
96-
#undef bool
97-
#endif
98-
9999
/* supply HeUTF8 if it's missing - ppport.h doesn't supply it, unfortunately */
100100
#ifndef HeUTF8
101101
#define HeUTF8(he) ((HeKLEN(he) == HEf_SVKEY) ? \

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy