Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12eee85

Browse files
committed
Make our usage of memset_s() conform strictly to the C11 standard.
Per the letter of the C11 standard, one must #define __STDC_WANT_LIB_EXT1__ as 1 before including <string.h> in order to have access to memset_s(). It appears that many platforms are lenient about this, because we weren't doing it and yet the code appeared to work anyway. But we now find that with -std=c11, macOS is strict and doesn't declare memset_s, leading to compile failures since we try to use it anyway. (Given the lack of prior reports, perhaps this is new behavior in the latest SDK? No matter, we're clearly in the wrong.) In addition to the immediate problem, which could be fixed merely by adding the needed #define to explicit_bzero.c, it seems possible that our configure-time probe for memset_s() could fail in case a platform implements the function in some odd way due to this spec requirement. This concern can be fixed in largely the same way that we dealt with strchrnul() in 6da2ba1: switch to using a declaration-based configure probe instead of a does-it-link probe. Back-patch to v13 where we started using memset_s(). Reported-by: Lakshmi Narayana Velayudam <[email protected]> Author: Tom Lane <[email protected]> Discussion: https://postgr.es/m/CAA4pTnLcKGG78xeOjiBr5yS7ZeE-Rh=FaFQQGOO=nPzA1L8yEA@mail.gmail.com Backpatch-through: 13
1 parent 0d4dad2 commit 12eee85

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

configure

+14-1
Original file line numberDiff line numberDiff line change
@@ -15616,7 +15616,7 @@ fi
1561615616
LIBS_including_readline="$LIBS"
1561715617
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
1561815618

15619-
for ac_func in backtrace_symbols copyfile copy_file_range elf_aux_info getauxval getifaddrs getpeerucred inet_pton kqueue localeconv_l mbstowcs_l memset_s posix_fallocate ppoll pthread_is_threaded_np setproctitle setproctitle_fast strsignal syncfs sync_file_range uselocale wcstombs_l
15619+
for ac_func in backtrace_symbols copyfile copy_file_range elf_aux_info getauxval getifaddrs getpeerucred inet_pton kqueue localeconv_l mbstowcs_l posix_fallocate ppoll pthread_is_threaded_np setproctitle setproctitle_fast strsignal syncfs sync_file_range uselocale wcstombs_l
1562015620
do :
1562115621
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
1562215622
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -16192,6 +16192,19 @@ cat >>confdefs.h <<_ACEOF
1619216192
#define HAVE_DECL_STRCHRNUL $ac_have_decl
1619316193
_ACEOF
1619416194

16195+
ac_fn_c_check_decl "$LINENO" "memset_s" "ac_cv_have_decl_memset_s" "#define __STDC_WANT_LIB_EXT1__ 1
16196+
#include <string.h>
16197+
"
16198+
if test "x$ac_cv_have_decl_memset_s" = xyes; then :
16199+
ac_have_decl=1
16200+
else
16201+
ac_have_decl=0
16202+
fi
16203+
16204+
cat >>confdefs.h <<_ACEOF
16205+
#define HAVE_DECL_MEMSET_S $ac_have_decl
16206+
_ACEOF
16207+
1619516208

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

configure.ac

+2-1
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,6 @@ AC_CHECK_FUNCS(m4_normalize([
17921792
kqueue
17931793
localeconv_l
17941794
mbstowcs_l
1795-
memset_s
17961795
posix_fallocate
17971796
ppoll
17981797
pthread_is_threaded_np
@@ -1838,6 +1837,8 @@ AC_CHECK_DECLS([strlcat, strlcpy, strnlen, strsep, timingsafe_bcmp])
18381837
AC_CHECK_DECLS([preadv], [], [], [#include <sys/uio.h>])
18391838
AC_CHECK_DECLS([pwritev], [], [], [#include <sys/uio.h>])
18401839
AC_CHECK_DECLS([strchrnul], [], [], [#include <string.h>])
1840+
AC_CHECK_DECLS([memset_s], [], [], [#define __STDC_WANT_LIB_EXT1__ 1
1841+
#include <string.h>])
18411842

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

meson.build

+8-6
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,7 @@ decl_checks += [
26542654
['preadv', 'sys/uio.h'],
26552655
['pwritev', 'sys/uio.h'],
26562656
['strchrnul', 'string.h'],
2657+
['memset_s', 'string.h', '#define __STDC_WANT_LIB_EXT1__ 1'],
26572658
]
26582659

26592660
# Check presence of some optional LLVM functions.
@@ -2667,21 +2668,23 @@ endif
26672668
foreach c : decl_checks
26682669
func = c.get(0)
26692670
header = c.get(1)
2670-
args = c.get(2, {})
2671+
prologue = c.get(2, '')
2672+
args = c.get(3, {})
26712673
varname = 'HAVE_DECL_' + func.underscorify().to_upper()
26722674

26732675
found = cc.compiles('''
2674-
#include <@0@>
2676+
@0@
2677+
#include <@1@>
26752678
26762679
int main()
26772680
{
2678-
#ifndef @1@
2679-
(void) @1@;
2681+
#ifndef @2@
2682+
(void) @2@;
26802683
#endif
26812684
26822685
return 0;
26832686
}
2684-
'''.format(header, func),
2687+
'''.format(prologue, header, func),
26852688
name: 'test whether @0@ is declared'.format(func),
26862689
# need to add cflags_warn to get at least
26872690
# -Werror=unguarded-availability-new if applicable
@@ -2880,7 +2883,6 @@ func_checks = [
28802883
['kqueue'],
28812884
['localeconv_l'],
28822885
['mbstowcs_l'],
2883-
['memset_s'],
28842886
['mkdtemp'],
28852887
['posix_fadvise'],
28862888
['posix_fallocate'],

src/include/pg_config.h.in

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
`LLVMCreatePerfJITEventListener', and to 0 if you don't. */
9292
#undef HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER
9393

94+
/* Define to 1 if you have the declaration of `memset_s', and to 0 if you
95+
don't. */
96+
#undef HAVE_DECL_MEMSET_S
97+
9498
/* Define to 1 if you have the declaration of `posix_fadvise', and to 0 if you
9599
don't. */
96100
#undef HAVE_DECL_POSIX_FADVISE
@@ -291,9 +295,6 @@
291295
/* Define to 1 if you have the <memory.h> header file. */
292296
#undef HAVE_MEMORY_H
293297

294-
/* Define to 1 if you have the `memset_s' function. */
295-
#undef HAVE_MEMSET_S
296-
297298
/* Define to 1 if you have the `mkdtemp' function. */
298299
#undef HAVE_MKDTEMP
299300

src/port/explicit_bzero.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
*-------------------------------------------------------------------------
1313
*/
1414

15+
#define __STDC_WANT_LIB_EXT1__ 1 /* needed to access memset_s() */
16+
1517
#include "c.h"
1618

17-
#if defined(HAVE_MEMSET_S)
19+
#if HAVE_DECL_MEMSET_S
1820

1921
void
2022
explicit_bzero(void *buf, size_t len)

0 commit comments

Comments
 (0)