From 85b47ab0e7b9f6e63ca1b632bb88c069211604a2 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Wed, 29 Apr 2009 11:38:31 +0000 Subject: [PATCH] define and use ber_strnlen() (ITS#6080); please regenerate configure and include/portable.hin --- configure.in | 1 + include/lber.h | 8 ++++++++ libraries/liblber/memory.c | 24 +++++++++++++++++++----- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/configure.in b/configure.in index 80975b8264..259dff7cb8 100644 --- a/configure.in +++ b/configure.in @@ -2480,6 +2480,7 @@ AC_CHECK_FUNCS( \ seteuid \ signal \ strdup \ + strnlen \ strpbrk \ strrchr \ strsep \ diff --git a/include/lber.h b/include/lber.h index cbd2d64745..e3a8e668a8 100644 --- a/include/lber.h +++ b/include/lber.h @@ -629,6 +629,14 @@ LBER_F( char * ) ber_strdup LDAP_P(( LDAP_CONST char * )); +LBER_F( ber_len_t ) +ber_strnlen LDAP_P(( + LDAP_CONST char *s, ber_len_t len )); + +LBER_F( char * ) +ber_strndup LDAP_P(( + LDAP_CONST char *s, ber_len_t l )); + LBER_F( struct berval * ) ber_bvreplace LDAP_P(( struct berval *dst, LDAP_CONST struct berval *src )); diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c index 6cf60ad284..933b6fb775 100644 --- a/libraries/liblber/memory.c +++ b/libraries/liblber/memory.c @@ -651,6 +651,24 @@ ber_strdup( LDAP_CONST char *s ) return ber_strdup_x( s, NULL ); } +ber_len_t +ber_strnlen( LDAP_CONST char *s, ber_len_t len ) +{ +#ifdef HAVE_STRNLEN + return (ber_len_t)strnlen( s, (ber_len_t)len ); +#else + ber_len_t l; + + for ( l = 0; l < len; l++ ) { + if ( s[l] == '\0' ) { + return l; + } + } + + return len; +#endif /* HAVE_STRNLEN */ +} + char * ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx ) { @@ -666,11 +684,7 @@ ber_strndup_x( LDAP_CONST char *s, ber_len_t l, void *ctx ) return NULL; } - len = strlen( s ); - - if ( len > l ) { - len = l; - } + len = ber_strnlen( s, l ); if ( (p = ber_memalloc_x( len + 1, ctx )) == NULL ) { ber_errno = LBER_ERROR_MEMORY; -- 2.39.5