Add pg_strnlen() a portable implementation of strlen.
authorAndres Freund <andres@anarazel.de>
Mon, 9 Oct 2017 22:20:42 +0000 (15:20 -0700)
committerAndres Freund <andres@anarazel.de>
Mon, 9 Oct 2017 22:20:42 +0000 (15:20 -0700)
As the OS version is likely going to be more optimized, fall back to
it if available, as detected by configure.

configure
configure.in
src/common/string.c
src/include/common/string.h
src/include/pg_config.h.in
src/include/pg_config.h.win32
src/port/snprintf.c

index 216447e73903d047ad34e9392cffed89375b2e65..a1283c05005ba1c851563d4b7350c70450597345 100755 (executable)
--- a/configure
+++ b/configure
@@ -8777,7 +8777,7 @@ fi
 
 
 
-for ac_func in strerror_r getpwuid_r gethostbyname_r
+for ac_func in strerror_r getpwuid_r gethostbyname_r strnlen
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
index a2e3d8331ad3f59ede2672036df07ea6f1ed5017..e1381b4ead693cb4b49758f3ddcf5aa7a843d386 100644 (file)
@@ -961,7 +961,7 @@ LIBS="$LIBS $PTHREAD_LIBS"
 AC_CHECK_HEADER(pthread.h, [], [AC_MSG_ERROR([
 pthread.h not found;  use --disable-thread-safety to disable thread safety])])
 
-AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r])
+AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r strnlen])
 
 # Do test here with the proper thread flags
 PGAC_FUNC_STRERROR_R_INT
index 159d9ea7b62a5758b45510355d5e10320775b151..901821f3d87e08cc0b55f3929747dcff86242e7c 100644 (file)
@@ -41,3 +41,23 @@ pg_str_endswith(const char *str, const char *end)
    str += slen - elen;
    return strcmp(str, end) == 0;
 }
+
+
+/*
+ * Portable version of posix' strnlen.
+ *
+ * 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.
+ */
+#ifndef HAVE_STRNLEN
+size_t
+pg_strnlen(const char *str, size_t maxlen)
+{
+   const char *p = str;
+
+   while (maxlen-- > 0 && *p)
+       p++;
+   return p - str;
+}
+#endif
index 5f3ea71d61329dae5d8e3ad8fb9ee2f3a623ee74..3d46b80918cff8e1ba9ddc7e5d6bbce94bec2765 100644 (file)
 
 extern bool pg_str_endswith(const char *str, const char *end);
 
+/*
+ * Portable version of posix' strnlen.
+ *
+ * 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.
+ *
+ * Use the system strnlen if provided, it's likely to be faster.
+ */
+#ifdef HAVE_STRNLEN
+#define pg_strnlen(str, maxlen) strnlen(str, maxlen)
+#else
+extern size_t pg_strnlen(const char *str, size_t maxlen);
+#endif
+
 #endif                         /* COMMON_STRING_H */
index 368a297e6df48f7e811d166cfae6f619894ffcde..d20cc47fde0b4483c6a75f3ad2464279c3cd99f4 100644 (file)
 /* 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 use have a strong random number source */
 #undef HAVE_STRONG_RANDOM
 
index 3537b6f7045ca48c758607d989d7cc60f4f5a31f..58eef0a538e79195c63609efb0c647dc7a0a416c 100644 (file)
 /* Define to 1 if you have the <string.h> header file. */
 #define HAVE_STRING_H 1
 
+/* Define to 1 if you have the `strnlen' function. */
+#define HAVE_STRNLEN
+
 /* Define to use have a strong random number source */
 #define HAVE_STRONG_RANDOM 1
 
index 231e5d6bdb427ccb570e55b5f5661dbf2b04574c..531d2c5ee35a92cb84b349a7156e25e0e335d056 100644 (file)
@@ -43,6 +43,8 @@
 #endif
 #include <sys/param.h>
 
+#include "common/string.h"
+
 #ifndef NL_ARGMAX
 #define NL_ARGMAX 16
 #endif
@@ -790,16 +792,6 @@ bad_format:
    target->failed = true;
 }
 
-static size_t
-pg_strnlen(const char *str, size_t maxlen)
-{
-   const char *p = str;
-
-   while (maxlen-- > 0 && *p)
-       p++;
-   return p - str;
-}
-
 static void
 fmtstr(char *value, int leftjust, int minlen, int maxwidth,
       int pointflag, PrintfTarget *target)