]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Support use of strnlen() in pre-v11 branches.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 7 Jun 2021 17:12:35 +0000 (13:12 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 7 Jun 2021 17:12:35 +0000 (13:12 -0400)
Back-patch a minimal subset of commits fffd651e8 and 46912d9b1,
to support strnlen() on all platforms without adding any callers.
This will be needed by a following bug fix.

configure
configure.in
src/include/pg_config.h.in
src/include/pg_config.h.win32
src/include/port.h
src/interfaces/libpq/.gitignore
src/interfaces/libpq/Makefile
src/port/strnlen.c [new file with mode: 0644]

index a8dea0c743d544e9993e61d0f472b0b91fb37b9b..5302f43e22239d562972c5372f590a52c1df2c57 100755 (executable)
--- a/configure
+++ b/configure
 cat >>confdefs.h <<_ACEOF
 #define HAVE_DECL_STRLCPY $ac_have_decl
 _ACEOF
+ac_fn_c_check_decl "$LINENO" "strnlen" "ac_cv_have_decl_strnlen" "$ac_includes_default"
+if test "x$ac_cv_have_decl_strnlen" = xyes; then :
+  ac_have_decl=1
+else
+  ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_STRNLEN $ac_have_decl
+_ACEOF
 
 # This is probably only present on Darwin, but may as well check always
 ac_fn_c_check_decl "$LINENO" "F_FULLFSYNC" "ac_cv_have_decl_F_FULLFSYNC" "#include <fcntl.h>
@@ -13282,6 +13292,19 @@ esac
 
 fi
 
+ac_fn_c_check_func "$LINENO" "strnlen" "ac_cv_func_strnlen"
+if test "x$ac_cv_func_strnlen" = xyes; then :
+  $as_echo "#define HAVE_STRNLEN 1" >>confdefs.h
+
+else
+  case " $LIBOBJS " in
+  *" strnlen.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS strnlen.$ac_objext"
+ ;;
+esac
+
+fi
+
 
 
 case $host_os in
index b096ac5fd330e698b62326e98eef6659d55ece9b..196a590e0e8d173330eef0398512633dd3cd7967 100644 (file)
@@ -1555,7 +1555,7 @@ AC_CHECK_DECLS(posix_fadvise, [], [], [#include <fcntl.h>])
 ]) # fi
 
 AC_CHECK_DECLS(fdatasync, [], [], [#include <unistd.h>])
-AC_CHECK_DECLS([strlcat, strlcpy])
+AC_CHECK_DECLS([strlcat, strlcpy, strnlen])
 # This is probably only present on Darwin, but may as well check always
 AC_CHECK_DECLS(F_FULLFSYNC, [], [], [#include <fcntl.h>])
 
@@ -1647,7 +1647,7 @@ else
   AC_CHECK_FUNCS([fpclass fp_class fp_class_d class], [break])
 fi
 
-AC_REPLACE_FUNCS([crypt fls getopt getrusage inet_aton mkdtemp random rint srandom strerror strlcat strlcpy])
+AC_REPLACE_FUNCS([crypt fls getopt getrusage inet_aton mkdtemp random rint srandom strerror strlcat strlcpy strnlen])
 
 case $host_os in
 
index adb0d336a336e2a509fa801097e5fe41af824228..9d2239ddb84bd4172f6fe4ce23771f1a23683fae 100644 (file)
    don't. */
 #undef HAVE_DECL_STRLCPY
 
+/* Define to 1 if you have the declaration of `strnlen', and to 0 if you
+   don't. */
+#undef HAVE_DECL_STRNLEN
+
 /* Define to 1 if you have the declaration of `strtoll', and to 0 if you
    don't. */
 #undef HAVE_DECL_STRTOLL
 /* Define to 1 if you have the `strlcpy' function. */
 #undef HAVE_STRLCPY
 
+/* Define to 1 if you have the `strnlen' function. */
+#undef HAVE_STRNLEN
+
 /* Define to 1 if you have the `strsignal' function. */
 #undef HAVE_STRSIGNAL
 
index 791029b865e63846adcc51b29e32c8c342715f29..e0ae4ee98953d884fa1a23f71beea7ce1653e765 100644 (file)
    don't. */
 #define HAVE_DECL_SNPRINTF 1
 
+/* Define to 1 if you have the declaration of `strnlen', and to 0 if you
+   don't. */
+#define HAVE_DECL_STRNLEN 1
+
 /* Define to 1 if you have the declaration of `strtoll', and to 0 if you
    don't. */
 #define HAVE_DECL_STRTOLL 1
 /* Define to 1 if you have the <pam/pam_appl.h> header file. */
 /* #undef HAVE_PAM_PAM_APPL_H */
 
+/* Define to 1 if you have the `strnlen' function. */
+#define HAVE_STRNLEN 1
+
 /* Define to 1 if you have the `poll' function. */
 /* #undef HAVE_POLL */
 
index 5ed20cef6b46157175f9ce9fad866ae57584344c..a0b8a770fb23e9bfe2790c8145238e5e5c676060 100644 (file)
@@ -404,6 +404,10 @@ extern size_t strlcat(char *dst, const char *src, size_t siz);
 extern size_t strlcpy(char *dst, const char *src, size_t siz);
 #endif
 
+#if !HAVE_DECL_STRNLEN
+extern size_t strnlen(const char *str, size_t maxlen);
+#endif
+
 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
 extern long random(void);
 #endif
index cb96af717665e776bb4c5d06f86263a9abcae895..ca6b667834190cd6872d2f1130af3c0bf3a12e3a 100644 (file)
@@ -12,6 +12,7 @@
 /snprintf.c
 /strerror.c
 /strlcpy.c
+/strnlen.c
 /system.c
 /thread.c
 /win32error.c
index 1b292d2cf22be1d30af4a83e9ad5cfe93eb50136..855d8abbc65d424f86695b0e8d50b55e5dcabc43 100644 (file)
@@ -38,7 +38,7 @@ OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
 OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o \
        thread.o
 # libpgport C files that are needed if identified by configure
-OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o system.o snprintf.o strerror.o strlcpy.o win32error.o win32setlocale.o, $(LIBOBJS))
+OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o system.o snprintf.o strerror.o strlcpy.o strnlen.o win32error.o win32setlocale.o, $(LIBOBJS))
 # backend/libpq
 OBJS += ip.o md5.o
 # utils/mb
@@ -93,7 +93,7 @@ backend_src = $(top_srcdir)/src/backend
 # For some libpgport modules, this only happens if configure decides
 # the module is needed (see filter hack in OBJS, above).
 
-chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
+chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c strnlen.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
        rm -f $@ && $(LN_S) $< .
 
 ip.c md5.c: % : $(backend_src)/libpq/%
@@ -145,7 +145,7 @@ clean distclean: clean-lib
 # Might be left over from a Win32 client-only build
        rm -f pg_config_paths.h
        rm -f inet_net_ntop.c noblock.c pgstrcasecmp.c pqsignal.c thread.c
-       rm -f chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c open.c system.c snprintf.c strerror.c strlcpy.c win32error.c win32setlocale.c
+       rm -f chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c open.c system.c snprintf.c strerror.c strlcpy.c strnlen.c win32error.c win32setlocale.c
        rm -f pgsleep.c
        rm -f md5.c ip.c
        rm -f encnames.c wchar.c
diff --git a/src/port/strnlen.c b/src/port/strnlen.c
new file mode 100644 (file)
index 0000000..260b883
--- /dev/null
@@ -0,0 +1,33 @@
+/*-------------------------------------------------------------------------
+ *
+ * strnlen.c
+ *             Fallback implementation of strnlen().
+ *
+ *
+ * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *       src/port/strnlen.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "c.h"
+
+/*
+ * Implementation of posix' strnlen for systems where it's not available.
+ *
+ * Returns the number of characters before a null-byte in the string pointed
+ * to by str, unless there's no null-byte before maxlen. In the latter case
+ * maxlen is returned.
+ */
+size_t
+strnlen(const char *str, size_t maxlen)
+{
+       const char *p = str;
+
+       while (maxlen-- > 0 && *p)
+               p++;
+       return p - str;
+}