Skip to content

Commit 467d77b

Browse files
committed
Cope with <regex.h> name clashes.
macOS 15's SDK pulls in headers related to <regex.h> when we include <xlocale.h>. This causes our own regex_t implementation to clash with the OS's regex_t implementation. Luckily our function names already had pg_ prefixes, but the macros and typenames did not. Include <regex.h> explicitly on all POSIX systems, and fix everything that breaks. Then we can prove that we are capable of fully hiding and replacing the system regex API with our own. 1. Deal with standard-clobbering macros by undefining them all first. POSIX says they are "symbolic constants". If they are macros, this allows us to redefine them. If they are enums or variables, our macros will hide them. 2. Deal with standard-clobbering types by giving our types pg_ prefixes, and then using macros to redirect xxx_t -> pg_xxx_t. After including our "regex/regex.h", the system <regex.h> is hidden, because we've replaced all the standard names. The PostgreSQL source tree and extensions can continue to use standard prefix-less type and macro names, but reach our implementation, if they included our "regex/regex.h" header. Back-patch to all supported branches, so that macOS 15's tool chain can build them. Reported-by: Stan Hu <stanhu@gmail.com> Suggested-by: Tom Lane <tgl@sss.pgh.pa.us> Tested-by: Aleksander Alekseev <aleksander@timescale.com> Discussion: https://postgr.es/m/CAMBWrQnEwEJtgOv7EUNsXmFw2Ub4p5P%2B5QTBEgYwiyjy7rAsEQ%40mail.gmail.com
1 parent 7961cd4 commit 467d77b

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-9
lines changed

src/include/regex/regex.h

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _REGEX_H_
2-
#define _REGEX_H_ /* never again */
1+
#ifndef _PG_REGEX_H_
2+
#define _PG_REGEX_H_ /* never again */
33
/*
44
* regular expressions
55
*
@@ -32,6 +32,84 @@
3232
* src/include/regex/regex.h
3333
*/
3434

35+
/*
36+
* This is an implementation of POSIX regex_t, so it clashes with the
37+
* system-provided <regex.h> header. That header might be unintentionally
38+
* included already, so we force that to happen now on all systems to show that
39+
* we can cope and that we completely replace the system regex interfaces.
40+
*
41+
* Note that we avoided using _REGEX_H_ as an include guard, as that confuses
42+
* matters on BSD family systems including macOS that use the same include
43+
* guard.
44+
*/
45+
#ifndef _WIN32
46+
#include <regex.h>
47+
#endif
48+
49+
/* Avoid redefinition errors due to the system header. */
50+
#undef REG_UBACKREF
51+
#undef REG_ULOOKAROUND
52+
#undef REG_UBOUNDS
53+
#undef REG_UBRACES
54+
#undef REG_UBSALNUM
55+
#undef REG_UPBOTCH
56+
#undef REG_UBBS
57+
#undef REG_UNONPOSIX
58+
#undef REG_UUNSPEC
59+
#undef REG_UUNPORT
60+
#undef REG_ULOCALE
61+
#undef REG_UEMPTYMATCH
62+
#undef REG_UIMPOSSIBLE
63+
#undef REG_USHORTEST
64+
#undef REG_BASIC
65+
#undef REG_EXTENDED
66+
#undef REG_ADVF
67+
#undef REG_ADVANCED
68+
#undef REG_QUOTE
69+
#undef REG_NOSPEC
70+
#undef REG_ICASE
71+
#undef REG_NOSUB
72+
#undef REG_EXPANDED
73+
#undef REG_NLSTOP
74+
#undef REG_NLANCH
75+
#undef REG_NEWLINE
76+
#undef REG_PEND
77+
#undef REG_EXPECT
78+
#undef REG_BOSONLY
79+
#undef REG_DUMP
80+
#undef REG_FAKE
81+
#undef REG_PROGRESS
82+
#undef REG_NOTBOL
83+
#undef REG_NOTEOL
84+
#undef REG_STARTEND
85+
#undef REG_FTRACE
86+
#undef REG_MTRACE
87+
#undef REG_SMALL
88+
#undef REG_OKAY
89+
#undef REG_NOMATCH
90+
#undef REG_BADPAT
91+
#undef REG_ECOLLATE
92+
#undef REG_ECTYPE
93+
#undef REG_EESCAPE
94+
#undef REG_ESUBREG
95+
#undef REG_EBRACK
96+
#undef REG_EPAREN
97+
#undef REG_EBRACE
98+
#undef REG_BADBR
99+
#undef REG_ERANGE
100+
#undef REG_ESPACE
101+
#undef REG_BADRPT
102+
#undef REG_ASSERT
103+
#undef REG_INVARG
104+
#undef REG_MIXED
105+
#undef REG_BADOPT
106+
#undef REG_ETOOBIG
107+
#undef REG_ECOLORS
108+
#undef REG_ATOI
109+
#undef REG_ITOA
110+
#undef REG_PREFIX
111+
#undef REG_EXACT
112+
35113
/*
36114
* Add your own defines, if needed, here.
37115
*/
@@ -45,7 +123,7 @@
45123
* regoff_t has to be large enough to hold either off_t or ssize_t,
46124
* and must be signed; it's only a guess that long is suitable.
47125
*/
48-
typedef long regoff_t;
126+
typedef long pg_regoff_t;
49127

50128
/*
51129
* other interface types
@@ -79,19 +157,19 @@ typedef struct
79157
/* the rest is opaque pointers to hidden innards */
80158
char *re_guts; /* `char *' is more portable than `void *' */
81159
char *re_fns;
82-
} regex_t;
160+
} pg_regex_t;
83161

84162
/* result reporting (may acquire more fields later) */
85163
typedef struct
86164
{
87-
regoff_t rm_so; /* start of substring */
88-
regoff_t rm_eo; /* end of substring */
89-
} regmatch_t;
165+
pg_regoff_t rm_so; /* start of substring */
166+
pg_regoff_t rm_eo; /* end of substring */
167+
} pg_regmatch_t;
90168

91169
/* supplementary control and reporting */
92170
typedef struct
93171
{
94-
regmatch_t rm_extend; /* see REG_EXPECT */
172+
pg_regmatch_t rm_extend; /* see REG_EXPECT */
95173
} rm_detail_t;
96174

97175

@@ -165,6 +243,11 @@ typedef struct
165243
#define REG_EXACT (-2) /* identified an exact match */
166244

167245

246+
/* Redirect the standard typenames to our typenames. */
247+
#define regoff_t pg_regoff_t
248+
#define regex_t pg_regex_t
249+
#define regmatch_t pg_regmatch_t
250+
168251

169252
/*
170253
* the prototypes for exported functions
@@ -183,4 +266,4 @@ extern bool RE_compile_and_execute(text *text_re, char *dat, int dat_len,
183266
int cflags, Oid collation,
184267
int nmatch, regmatch_t *pmatch);
185268

186-
#endif /* _REGEX_H_ */
269+
#endif /* _PG_REGEX_H_ */

src/tools/pgindent/typedefs.list

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3484,6 +3484,9 @@ pg_md5_ctx
34843484
pg_on_exit_callback
34853485
pg_prng_state
34863486
pg_re_flags
3487+
pg_regex_t
3488+
pg_regmatch_t
3489+
pg_regoff_t
34873490
pg_saslprep_rc
34883491
pg_sha1_ctx
34893492
pg_sha224_ctx

0 commit comments

Comments
 (0)