]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/charray.c
Fix off by one bug
[openldap] / libraries / libldap / charray.c
index 82d2feeb1ba264e09413991ad54c337addb7d220..41432d21b4d21dffd10025406c15951b512bfbd4 100644 (file)
@@ -221,25 +221,26 @@ ldap_str2charray( const char *str_in, const char *brkstr )
 char * ldap_charray2str( char **a, const char *sep )
 {
        char *s, **v, *p;
-       int len = 0;
+       int len;
        int slen;
 
        if( sep == NULL ) sep = " ";
 
        slen = strlen( sep );
+       len = 0;
 
        for ( v = a; *v != NULL; v++ ) {
-               len += strlen( *v ) + slen; /* for a space */
+               len += strlen( *v ) + slen;
        }
 
        if ( len == 0 ) {
                return NULL;
        }
 
+       /* trim extra sep len */
        len -= slen;
-       len += 1; /* EOS */
 
-       s = LDAP_MALLOC ( len );
+       s = LDAP_MALLOC ( len + 1 );
 
        if ( s == NULL ) {
                return NULL;    
@@ -247,8 +248,6 @@ char * ldap_charray2str( char **a, const char *sep )
 
        p = s;
        for ( v = a; *v != NULL; v++ ) {
-               int len;
-
                if ( v != a ) {
                        strncpy( p, sep, slen );
                        p += slen;