Skip to content

Commit e4440a7

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 db8238d commit e4440a7

File tree

5 files changed

+135
-22
lines changed

5 files changed

+135
-22
lines changed

configure

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5448,6 +5448,98 @@ if test x"$pgac_cv_prog_CC_cflags__Werror_vla" = x"yes"; then
54485448
fi
54495449

54505450

5451+
# On macOS, complain about usage of symbols newer than the deployment target
5452+
5453+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CC} supports -Werror=unguarded-availability-new, for CFLAGS" >&5
5454+
$as_echo_n "checking whether ${CC} supports -Werror=unguarded-availability-new, for CFLAGS... " >&6; }
5455+
if ${pgac_cv_prog_CC_cflags__Werror_unguarded_availability_new+:} false; then :
5456+
$as_echo_n "(cached) " >&6
5457+
else
5458+
pgac_save_CFLAGS=$CFLAGS
5459+
pgac_save_CC=$CC
5460+
CC=${CC}
5461+
CFLAGS="${CFLAGS} -Werror=unguarded-availability-new"
5462+
ac_save_c_werror_flag=$ac_c_werror_flag
5463+
ac_c_werror_flag=yes
5464+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
5465+
/* end confdefs.h. */
5466+
5467+
int
5468+
main ()
5469+
{
5470+
5471+
;
5472+
return 0;
5473+
}
5474+
_ACEOF
5475+
if ac_fn_c_try_compile "$LINENO"; then :
5476+
pgac_cv_prog_CC_cflags__Werror_unguarded_availability_new=yes
5477+
else
5478+
pgac_cv_prog_CC_cflags__Werror_unguarded_availability_new=no
5479+
fi
5480+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
5481+
ac_c_werror_flag=$ac_save_c_werror_flag
5482+
CFLAGS="$pgac_save_CFLAGS"
5483+
CC="$pgac_save_CC"
5484+
fi
5485+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_prog_CC_cflags__Werror_unguarded_availability_new" >&5
5486+
$as_echo "$pgac_cv_prog_CC_cflags__Werror_unguarded_availability_new" >&6; }
5487+
if test x"$pgac_cv_prog_CC_cflags__Werror_unguarded_availability_new" = x"yes"; then
5488+
CFLAGS="${CFLAGS} -Werror=unguarded-availability-new"
5489+
fi
5490+
5491+
5492+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CXX} supports -Werror=unguarded-availability-new, for CXXFLAGS" >&5
5493+
$as_echo_n "checking whether ${CXX} supports -Werror=unguarded-availability-new, for CXXFLAGS... " >&6; }
5494+
if ${pgac_cv_prog_CXX_cxxflags__Werror_unguarded_availability_new+:} false; then :
5495+
$as_echo_n "(cached) " >&6
5496+
else
5497+
pgac_save_CXXFLAGS=$CXXFLAGS
5498+
pgac_save_CXX=$CXX
5499+
CXX=${CXX}
5500+
CXXFLAGS="${CXXFLAGS} -Werror=unguarded-availability-new"
5501+
ac_save_cxx_werror_flag=$ac_cxx_werror_flag
5502+
ac_cxx_werror_flag=yes
5503+
ac_ext=cpp
5504+
ac_cpp='$CXXCPP $CPPFLAGS'
5505+
ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
5506+
ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
5507+
ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
5508+
5509+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
5510+
/* end confdefs.h. */
5511+
5512+
int
5513+
main ()
5514+
{
5515+
5516+
;
5517+
return 0;
5518+
}
5519+
_ACEOF
5520+
if ac_fn_cxx_try_compile "$LINENO"; then :
5521+
pgac_cv_prog_CXX_cxxflags__Werror_unguarded_availability_new=yes
5522+
else
5523+
pgac_cv_prog_CXX_cxxflags__Werror_unguarded_availability_new=no
5524+
fi
5525+
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
5526+
ac_ext=c
5527+
ac_cpp='$CPP $CPPFLAGS'
5528+
ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
5529+
ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
5530+
ac_compiler_gnu=$ac_cv_c_compiler_gnu
5531+
5532+
ac_cxx_werror_flag=$ac_save_cxx_werror_flag
5533+
CXXFLAGS="$pgac_save_CXXFLAGS"
5534+
CXX="$pgac_save_CXX"
5535+
fi
5536+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_prog_CXX_cxxflags__Werror_unguarded_availability_new" >&5
5537+
$as_echo "$pgac_cv_prog_CXX_cxxflags__Werror_unguarded_availability_new" >&6; }
5538+
if test x"$pgac_cv_prog_CXX_cxxflags__Werror_unguarded_availability_new" = x"yes"; then
5539+
CXXFLAGS="${CXXFLAGS} -Werror=unguarded-availability-new"
5540+
fi
5541+
5542+
54515543
# -Wvla is not applicable for C++
54525544

54535545
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether ${CC} supports -Wendif-labels, for CFLAGS" >&5
@@ -15697,7 +15789,7 @@ fi
1569715789
LIBS_including_readline="$LIBS"
1569815790
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
1569915791

15700-
for ac_func in backtrace_symbols clock_gettime copyfile fdatasync getifaddrs getpeerucred getrlimit kqueue mbstowcs_l memset_s poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open strchrnul strsignal symlink sync_file_range uselocale wcstombs_l
15792+
for ac_func in backtrace_symbols clock_gettime copyfile fdatasync getifaddrs getpeerucred getrlimit kqueue mbstowcs_l memset_s poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open strsignal symlink sync_file_range uselocale wcstombs_l
1570115793
do :
1570215794
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
1570315795
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -16214,6 +16306,22 @@ cat >>confdefs.h <<_ACEOF
1621416306
#define HAVE_DECL_STRNLEN $ac_have_decl
1621516307
_ACEOF
1621616308

16309+
16310+
# We can't use AC_REPLACE_FUNCS to replace these functions, because it
16311+
# won't handle deployment target restrictions on macOS
16312+
ac_fn_c_check_decl "$LINENO" "strchrnul" "ac_cv_have_decl_strchrnul" "#include <string.h>
16313+
"
16314+
if test "x$ac_cv_have_decl_strchrnul" = xyes; then :
16315+
ac_have_decl=1
16316+
else
16317+
ac_have_decl=0
16318+
fi
16319+
16320+
cat >>confdefs.h <<_ACEOF
16321+
#define HAVE_DECL_STRCHRNUL $ac_have_decl
16322+
_ACEOF
16323+
16324+
1621716325
# This is probably only present on macOS, but may as well check always
1621816326
ac_fn_c_check_decl "$LINENO" "F_FULLFSYNC" "ac_cv_have_decl_F_FULLFSYNC" "#include <fcntl.h>
1621916327
"

configure.in

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,9 @@ if test "$GCC" = yes -a "$ICC" = no; then
511511
AC_SUBST(PERMIT_DECLARATION_AFTER_STATEMENT)
512512
# Really don't want VLAs to be used in our dialect of C
513513
PGAC_PROG_CC_CFLAGS_OPT([-Werror=vla])
514+
# On macOS, complain about usage of symbols newer than the deployment target
515+
PGAC_PROG_CC_CFLAGS_OPT([-Werror=unguarded-availability-new])
516+
PGAC_PROG_CXX_CFLAGS_OPT([-Werror=unguarded-availability-new])
514517
# -Wvla is not applicable for C++
515518
PGAC_PROG_CC_CFLAGS_OPT([-Wendif-labels])
516519
PGAC_PROG_CXX_CFLAGS_OPT([-Wendif-labels])
@@ -1758,7 +1761,6 @@ AC_CHECK_FUNCS(m4_normalize([
17581761
setproctitle_fast
17591762
setsid
17601763
shm_open
1761-
strchrnul
17621764
strsignal
17631765
symlink
17641766
sync_file_range
@@ -1793,6 +1795,11 @@ AC_CHECK_DECLS(posix_fadvise, [], [], [#include <fcntl.h>])
17931795

17941796
AC_CHECK_DECLS(fdatasync, [], [], [#include <unistd.h>])
17951797
AC_CHECK_DECLS([strlcat, strlcpy, strnlen])
1798+
1799+
# We can't use AC_REPLACE_FUNCS to replace these functions, because it
1800+
# won't handle deployment target restrictions on macOS
1801+
AC_CHECK_DECLS([strchrnul], [], [], [#include <string.h>])
1802+
17961803
# This is probably only present on macOS, but may as well check always
17971804
AC_CHECK_DECLS(F_FULLFSYNC, [], [], [#include <fcntl.h>])
17981805

src/include/pg_config.h.in

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@
147147
don't. */
148148
#undef HAVE_DECL_RTLD_NOW
149149

150+
/* Define to 1 if you have the declaration of `strchrnul', and to 0 if you
151+
don't. */
152+
#undef HAVE_DECL_STRCHRNUL
153+
150154
/* Define to 1 if you have the declaration of `strlcat', and to 0 if you
151155
don't. */
152156
#undef HAVE_DECL_STRLCAT
@@ -497,9 +501,6 @@
497501
/* Define to 1 if you have the <stdlib.h> header file. */
498502
#undef HAVE_STDLIB_H
499503

500-
/* Define to 1 if you have the `strchrnul' function. */
501-
#undef HAVE_STRCHRNUL
502-
503504
/* Define to 1 if you have the `strerror_r' function. */
504505
#undef HAVE_STRERROR_R
505506

src/port/snprintf.c

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

350350
/*
351-
* If strchrnul exists (it's a glibc-ism), it's a good bit faster than the
352-
* equivalent manual loop. If it doesn't exist, provide a replacement.
351+
* If strchrnul exists (it's a glibc-ism, but since adopted by some other
352+
* platforms), it's a good bit faster than the equivalent manual loop.
353+
* Use it if possible, and if it doesn't exist, use this replacement.
353354
*
354355
* Note: glibc declares this as returning "char *", but that would require
355356
* casting away const internally, so we don't follow that detail.
357+
*
358+
* Note: macOS has this too as of Sequoia 15.4, but it's hidden behind
359+
* a deployment-target check that causes compile errors if the deployment
360+
* target isn't high enough. So !HAVE_DECL_STRCHRNUL may mean "yes it's
361+
* declared, but it doesn't compile". To avoid failing in that scenario,
362+
* use a macro to avoid matching <string.h>'s name.
356363
*/
357-
#ifndef HAVE_STRCHRNUL
364+
#if !HAVE_DECL_STRCHRNUL
365+
366+
#define strchrnul pg_strchrnul
358367

359368
static inline const char *
360369
strchrnul(const char *s, int c)
@@ -364,19 +373,7 @@ strchrnul(const char *s, int c)
364373
return s;
365374
}
366375

367-
#else
368-
369-
/*
370-
* glibc's <string.h> declares strchrnul only if _GNU_SOURCE is defined.
371-
* While we typically use that on glibc platforms, configure will set
372-
* HAVE_STRCHRNUL whether it's used or not. Fill in the missing declaration
373-
* so that this file will compile cleanly with or without _GNU_SOURCE.
374-
*/
375-
#ifndef _GNU_SOURCE
376-
extern char *strchrnul(const char *s, int c);
377-
#endif
378-
379-
#endif /* HAVE_STRCHRNUL */
376+
#endif /* !HAVE_DECL_STRCHRNUL */
380377

381378

382379
/*

src/tools/msvc/Solution.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ sub GenerateFiles
243243
HAVE_DECL_POSIX_FADVISE => 0,
244244
HAVE_DECL_RTLD_GLOBAL => 0,
245245
HAVE_DECL_RTLD_NOW => 0,
246+
HAVE_DECL_STRCHRNUL => 0,
246247
HAVE_DECL_STRLCAT => 0,
247248
HAVE_DECL_STRLCPY => 0,
248249
HAVE_DECL_STRNLEN => 1,
@@ -356,7 +357,6 @@ sub GenerateFiles
356357
HAVE_SRANDOM => undef,
357358
HAVE_STDINT_H => 1,
358359
HAVE_STDLIB_H => 1,
359-
HAVE_STRCHRNUL => undef,
360360
HAVE_STRERROR_R => undef,
361361
HAVE_STRINGS_H => undef,
362362
HAVE_STRING_H => 1,

0 commit comments

Comments
 (0)