aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndres Freund <andres@anarazel.de>2017-10-10 14:42:16 -0700
committerAndres Freund <andres@anarazel.de>2017-10-10 14:50:30 -0700
commitfffd651e83ccbd6191a76be6ec7c6b1b27888fde (patch)
tree1c7f61fd5dfa8e540f2e27c1063ba9f8038370ba /src
parentfa5e119dc71ada8d023deadcb36dbfae328f8902 (diff)
downloadpostgresql-fffd651e83ccbd6191a76be6ec7c6b1b27888fde.tar.gz
postgresql-fffd651e83ccbd6191a76be6ec7c6b1b27888fde.zip
Rewrite strnlen replacement implementation from 8a241792f96.
The previous placement of the fallback implementation in libpgcommon was problematic, because libpqport functions need strnlen functionality. Move replacement into libpgport. Provide strnlen() under its posix name, instead of pg_strnlen(). Fix stupid configure bug, executing the test only when compiled with threading support. Author: Andres Freund Discussion: https://postgr.es/m/E1e1gR2-0005fB-SI@gemulon.postgresql.org
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/mmgr/mcxt.c3
-rw-r--r--src/common/string.c20
-rw-r--r--src/include/common/string.h15
-rw-r--r--src/include/pg_config.h.in4
-rw-r--r--src/include/pg_config.h.win3210
-rw-r--r--src/include/port.h4
-rw-r--r--src/port/snprintf.c4
-rw-r--r--src/port/strnlen.c33
8 files changed, 50 insertions, 43 deletions
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
index 64e0408d5af..c5c311fad39 100644
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -21,7 +21,6 @@
#include "postgres.h"
-#include "common/string.h"
#include "miscadmin.h"
#include "utils/memdebug.h"
#include "utils/memutils.h"
@@ -1089,7 +1088,7 @@ pnstrdup(const char *in, Size len)
{
char *out;
- len = pg_strnlen(in, len);
+ len = strnlen(in, len);
out = palloc(len + 1);
memcpy(out, in, len);
diff --git a/src/common/string.c b/src/common/string.c
index 901821f3d87..159d9ea7b62 100644
--- a/src/common/string.c
+++ b/src/common/string.c
@@ -41,23 +41,3 @@ 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
diff --git a/src/include/common/string.h b/src/include/common/string.h
index 3d46b80918c..5f3ea71d613 100644
--- a/src/include/common/string.h
+++ b/src/include/common/string.h
@@ -12,19 +12,4 @@
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 */
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index d20cc47fde0..b0298cca19c 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -147,6 +147,10 @@
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 `sys_siglist', and to 0 if you
don't. */
#undef HAVE_DECL_SYS_SIGLIST
diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32
index 58eef0a538e..b76aad02676 100644
--- a/src/include/pg_config.h.win32
+++ b/src/include/pg_config.h.win32
@@ -99,6 +99,10 @@
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 `vsnprintf', and to 0 if you
don't. */
#define HAVE_DECL_VSNPRINTF 1
@@ -255,6 +259,9 @@
/* 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 */
@@ -345,9 +352,6 @@
/* 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
diff --git a/src/include/port.h b/src/include/port.h
index b1ba645655f..17a7710a5e2 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -406,6 +406,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)
extern long random(void);
#endif
diff --git a/src/port/snprintf.c b/src/port/snprintf.c
index 531d2c5ee35..43c17e702e2 100644
--- a/src/port/snprintf.c
+++ b/src/port/snprintf.c
@@ -43,8 +43,6 @@
#endif
#include <sys/param.h>
-#include "common/string.h"
-
#ifndef NL_ARGMAX
#define NL_ARGMAX 16
#endif
@@ -804,7 +802,7 @@ fmtstr(char *value, int leftjust, int minlen, int maxwidth,
* than that.
*/
if (pointflag)
- vallen = pg_strnlen(value, maxwidth);
+ vallen = strnlen(value, maxwidth);
else
vallen = strlen(value);
diff --git a/src/port/strnlen.c b/src/port/strnlen.c
new file mode 100644
index 00000000000..260b883368b
--- /dev/null
+++ b/src/port/strnlen.c
@@ -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;
+}