Skip to content

Commit 8a24179

Browse files
committed
Add pg_strnlen() a portable implementation of strlen.
As the OS version is likely going to be more optimized, fall back to it if available, as detected by configure.
1 parent 71c75dd commit 8a24179

File tree

7 files changed

+45
-12
lines changed

7 files changed

+45
-12
lines changed

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8777,7 +8777,7 @@ fi
87778777

87788778

87798779

8780-
for ac_func in strerror_r getpwuid_r gethostbyname_r
8780+
for ac_func in strerror_r getpwuid_r gethostbyname_r strnlen
87818781
do :
87828782
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
87838783
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"

configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ LIBS="$LIBS $PTHREAD_LIBS"
961961
AC_CHECK_HEADER(pthread.h, [], [AC_MSG_ERROR([
962962
pthread.h not found; use --disable-thread-safety to disable thread safety])])
963963

964-
AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r])
964+
AC_CHECK_FUNCS([strerror_r getpwuid_r gethostbyname_r strnlen])
965965

966966
# Do test here with the proper thread flags
967967
PGAC_FUNC_STRERROR_R_INT

src/common/string.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ pg_str_endswith(const char *str, const char *end)
4141
str += slen - elen;
4242
return strcmp(str, end) == 0;
4343
}
44+
45+
46+
/*
47+
* Portable version of posix' strnlen.
48+
*
49+
* Returns the number of characters before a null-byte in the string pointed
50+
* to by str, unless there's no null-byte before maxlen. In the latter case
51+
* maxlen is returned.
52+
*/
53+
#ifndef HAVE_STRNLEN
54+
size_t
55+
pg_strnlen(const char *str, size_t maxlen)
56+
{
57+
const char *p = str;
58+
59+
while (maxlen-- > 0 && *p)
60+
p++;
61+
return p - str;
62+
}
63+
#endif

src/include/common/string.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@
1212

1313
extern bool pg_str_endswith(const char *str, const char *end);
1414

15+
/*
16+
* Portable version of posix' strnlen.
17+
*
18+
* Returns the number of characters before a null-byte in the string pointed
19+
* to by str, unless there's no null-byte before maxlen. In the latter case
20+
* maxlen is returned.
21+
*
22+
* Use the system strnlen if provided, it's likely to be faster.
23+
*/
24+
#ifdef HAVE_STRNLEN
25+
#define pg_strnlen(str, maxlen) strnlen(str, maxlen)
26+
#else
27+
extern size_t pg_strnlen(const char *str, size_t maxlen);
28+
#endif
29+
1530
#endif /* COMMON_STRING_H */

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,9 @@
496496
/* Define to 1 if you have the `strlcpy' function. */
497497
#undef HAVE_STRLCPY
498498

499+
/* Define to 1 if you have the `strnlen' function. */
500+
#undef HAVE_STRNLEN
501+
499502
/* Define to use have a strong random number source */
500503
#undef HAVE_STRONG_RANDOM
501504

src/include/pg_config.h.win32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@
345345
/* Define to 1 if you have the <string.h> header file. */
346346
#define HAVE_STRING_H 1
347347

348+
/* Define to 1 if you have the `strnlen' function. */
349+
#define HAVE_STRNLEN
350+
348351
/* Define to use have a strong random number source */
349352
#define HAVE_STRONG_RANDOM 1
350353

src/port/snprintf.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
#endif
4444
#include <sys/param.h>
4545

46+
#include "common/string.h"
47+
4648
#ifndef NL_ARGMAX
4749
#define NL_ARGMAX 16
4850
#endif
@@ -790,16 +792,6 @@ dopr(PrintfTarget *target, const char *format, va_list args)
790792
target->failed = true;
791793
}
792794

793-
static size_t
794-
pg_strnlen(const char *str, size_t maxlen)
795-
{
796-
const char *p = str;
797-
798-
while (maxlen-- > 0 && *p)
799-
p++;
800-
return p - str;
801-
}
802-
803795
static void
804796
fmtstr(char *value, int leftjust, int minlen, int maxwidth,
805797
int pointflag, PrintfTarget *target)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy