Skip to content

Commit 915e889

Browse files
tglsfdcpetere
andcommitted
Fix detection and handling of strchrnul() for macOS 15.4.
As of 15.4, macOS has strchrnul(), but access to it is blocked behind a check for MACOSX_DEPLOYMENT_TARGET >= 15.4. But our does-it-link configure check finds it, so we try to use it, and fail with the present default deployment target (namely 15.0). This accounts for today's buildfarm failures on indri and sifaka. This is the identical problem that we faced some years ago when Apple introduced preadv and pwritev in the same way. We solved that in commit f014b1b by using AC_CHECK_DECLS instead of AC_CHECK_FUNCS to check the functions' availability. So do the same now for strchrnul(). Interestingly, we already had a workaround for "the link check doesn't agree with <string.h>" cases with glibc, which we no longer need since only the header declaration is being checked. Testing this revealed that the meson version of this check has never worked, because it failed to use "-Werror=unguarded-availability-new". (Apparently nobody's tried to build with meson on macOS versions that lack preadv/pwritev as standard.) Adjust that while at it. Also, we had never put support for "-Werror=unguarded-availability-new" into v13, but we need that now. Co-authored-by: Tom Lane <tgl@sss.pgh.pa.us> Co-authored-by: Peter Eisentraut <peter@eisentraut.org> Discussion: https://postgr.es/m/385134.1743523038@sss.pgh.pa.us Backpatch-through: 13
1 parent 2530367 commit 915e889

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

configure

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15272,7 +15272,7 @@ fi
1527215272
LIBS_including_readline="$LIBS"
1527315273
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
1527415274

15275-
for ac_func in backtrace_symbols copyfile copy_file_range getifaddrs getpeerucred inet_pton kqueue mbstowcs_l memset_s posix_fallocate ppoll pthread_is_threaded_np setproctitle setproctitle_fast strchrnul strsignal syncfs sync_file_range uselocale wcstombs_l
15275+
for ac_func in backtrace_symbols copyfile copy_file_range getifaddrs getpeerucred inet_pton kqueue mbstowcs_l memset_s posix_fallocate ppoll pthread_is_threaded_np setproctitle setproctitle_fast strsignal syncfs sync_file_range uselocale wcstombs_l
1527615276
do :
1527715277
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
1527815278
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -15816,6 +15816,18 @@ cat >>confdefs.h <<_ACEOF
1581615816
#define HAVE_DECL_PWRITEV $ac_have_decl
1581715817
_ACEOF
1581815818

15819+
ac_fn_c_check_decl "$LINENO" "strchrnul" "ac_cv_have_decl_strchrnul" "#include <string.h>
15820+
"
15821+
if test "x$ac_cv_have_decl_strchrnul" = xyes; then :
15822+
ac_have_decl=1
15823+
else
15824+
ac_have_decl=0
15825+
fi
15826+
15827+
cat >>confdefs.h <<_ACEOF
15828+
#define HAVE_DECL_STRCHRNUL $ac_have_decl
15829+
_ACEOF
15830+
1581915831

1582015832
# This is probably only present on macOS, but may as well check always
1582115833
ac_fn_c_check_decl "$LINENO" "F_FULLFSYNC" "ac_cv_have_decl_F_FULLFSYNC" "#include <fcntl.h>

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,6 @@ AC_CHECK_FUNCS(m4_normalize([
17631763
pthread_is_threaded_np
17641764
setproctitle
17651765
setproctitle_fast
1766-
strchrnul
17671766
strsignal
17681767
syncfs
17691768
sync_file_range
@@ -1803,6 +1802,7 @@ AC_CHECK_DECLS([strlcat, strlcpy, strnlen])
18031802
# won't handle deployment target restrictions on macOS
18041803
AC_CHECK_DECLS([preadv], [], [], [#include <sys/uio.h>])
18051804
AC_CHECK_DECLS([pwritev], [], [], [#include <sys/uio.h>])
1805+
AC_CHECK_DECLS([strchrnul], [], [], [#include <string.h>])
18061806

18071807
# This is probably only present on macOS, but may as well check always
18081808
AC_CHECK_DECLS(F_FULLFSYNC, [], [], [#include <fcntl.h>])

meson.build

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,7 @@ decl_checks = [
24692469
decl_checks += [
24702470
['preadv', 'sys/uio.h'],
24712471
['pwritev', 'sys/uio.h'],
2472+
['strchrnul', 'string.h'],
24722473
]
24732474

24742475
# Check presence of some optional LLVM functions.
@@ -2485,8 +2486,23 @@ foreach c : decl_checks
24852486
args = c.get(2, {})
24862487
varname = 'HAVE_DECL_' + func.underscorify().to_upper()
24872488

2488-
found = cc.has_header_symbol(header, func,
2489-
args: test_c_args, include_directories: postgres_inc,
2489+
found = cc.compiles('''
2490+
#include <@0@>
2491+
2492+
int main()
2493+
{
2494+
#ifndef @1@
2495+
(void) @1@;
2496+
#endif
2497+
2498+
return 0;
2499+
}
2500+
'''.format(header, func),
2501+
name: 'test whether @0@ is declared'.format(func),
2502+
# need to add cflags_warn to get at least
2503+
# -Werror=unguarded-availability-new if applicable
2504+
args: test_c_args + cflags_warn,
2505+
include_directories: postgres_inc,
24902506
kwargs: args)
24912507
cdata.set10(varname, found, description:
24922508
'''Define to 1 if you have the declaration of `@0@', and to 0 if you
@@ -2724,7 +2740,6 @@ func_checks = [
27242740
['shm_unlink', {'dependencies': [rt_dep], 'define': false}],
27252741
['shmget', {'dependencies': [cygipc_dep], 'define': false}],
27262742
['socket', {'dependencies': [socket_dep], 'define': false}],
2727-
['strchrnul'],
27282743
['strerror_r', {'dependencies': [thread_dep]}],
27292744
['strlcat'],
27302745
['strlcpy'],

src/include/pg_config.h.in

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@
115115
don't. */
116116
#undef HAVE_DECL_PWRITEV
117117

118+
/* Define to 1 if you have the declaration of `strchrnul', and to 0 if you
119+
don't. */
120+
#undef HAVE_DECL_STRCHRNUL
121+
118122
/* Define to 1 if you have the declaration of `strlcat', and to 0 if you
119123
don't. */
120124
#undef HAVE_DECL_STRLCAT
@@ -393,9 +397,6 @@
393397
/* Define to 1 if you have the <stdlib.h> header file. */
394398
#undef HAVE_STDLIB_H
395399

396-
/* Define to 1 if you have the `strchrnul' function. */
397-
#undef HAVE_STRCHRNUL
398-
399400
/* Define to 1 if you have the `strerror_r' function. */
400401
#undef HAVE_STRERROR_R
401402

src/port/snprintf.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,22 @@ static void leading_pad(int zpad, int signvalue, int *padlen,
338338
static void trailing_pad(int padlen, PrintfTarget *target);
339339

340340
/*
341-
* If strchrnul exists (it's a glibc-ism), it's a good bit faster than the
342-
* equivalent manual loop. If it doesn't exist, provide a replacement.
341+
* If strchrnul exists (it's a glibc-ism, but since adopted by some other
342+
* platforms), it's a good bit faster than the equivalent manual loop.
343+
* Use it if possible, and if it doesn't exist, use this replacement.
343344
*
344345
* Note: glibc declares this as returning "char *", but that would require
345346
* casting away const internally, so we don't follow that detail.
347+
*
348+
* Note: macOS has this too as of Sequoia 15.4, but it's hidden behind
349+
* a deployment-target check that causes compile errors if the deployment
350+
* target isn't high enough. So !HAVE_DECL_STRCHRNUL may mean "yes it's
351+
* declared, but it doesn't compile". To avoid failing in that scenario,
352+
* use a macro to avoid matching <string.h>'s name.
346353
*/
347-
#ifndef HAVE_STRCHRNUL
354+
#if !HAVE_DECL_STRCHRNUL
355+
356+
#define strchrnul pg_strchrnul
348357

349358
static inline const char *
350359
strchrnul(const char *s, int c)
@@ -354,19 +363,7 @@ strchrnul(const char *s, int c)
354363
return s;
355364
}
356365

357-
#else
358-
359-
/*
360-
* glibc's <string.h> declares strchrnul only if _GNU_SOURCE is defined.
361-
* While we typically use that on glibc platforms, configure will set
362-
* HAVE_STRCHRNUL whether it's used or not. Fill in the missing declaration
363-
* so that this file will compile cleanly with or without _GNU_SOURCE.
364-
*/
365-
#ifndef _GNU_SOURCE
366-
extern char *strchrnul(const char *s, int c);
367-
#endif
368-
369-
#endif /* HAVE_STRCHRNUL */
366+
#endif /* !HAVE_DECL_STRCHRNUL */
370367

371368

372369
/*

0 commit comments

Comments
 (0)