X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Flibldap%2Furl.c;h=f681a72533ac1fff1fe1be18a631be46ffbf5aa2;hb=e22dc83ac096d1239a6903199f5dc3231b0f444b;hp=6a6e076b56fa16c8a13a0b4f1f12a714639ffb6e;hpb=4e32148ac59ba6b4f3acc79a3b782cd3556f941a;p=openldap diff --git a/libraries/libldap/url.c b/libraries/libldap/url.c index 6a6e076b56..f681a72533 100644 --- a/libraries/libldap/url.c +++ b/libraries/libldap/url.c @@ -2,7 +2,7 @@ /* $OpenLDAP$ */ /* This work is part of OpenLDAP Software . * - * Copyright 1998-2007 The OpenLDAP Foundation. + * Copyright 1998-2009 The OpenLDAP Foundation. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -582,19 +582,19 @@ desc2str_len( LDAPURLDesc *u ) len += sep; if ( u->lud_port ) { - char buf[] = ":65535"; + unsigned p = u->lud_port; + if ( p > 65535 ) + return -1; - len += snprintf( buf, sizeof( buf ), ":%d", u->lud_port ); - if ( u->lud_host && u->lud_host[0] ) { - len += strlen( u->lud_host ); - } + len += (p > 999 ? 5 + (p > 9999) : p > 99 ? 4 : 2 + (p > 9)); + } - } else { - if ( u->lud_host && u->lud_host[0] ) { - len += hex_escape_len( u->lud_host, URLESC_SLASH ); - if ( !is_ipc && strchr( u->lud_host, ':' )) { + if ( u->lud_host && u->lud_host[0] ) { + char *ptr; + len += hex_escape_len( u->lud_host, URLESC_SLASH ); + if ( !is_ipc && ( ptr = strchr( u->lud_host, ':' ))) { + if ( strchr( ptr+1, ':' )) len += 2; /* IPv6, [] */ - } } } @@ -612,6 +612,7 @@ desc2str( LDAPURLDesc *u, char *s, int len ) int is_v6 = 0; int is_ipc = 0; struct berval scope = BER_BVNULL; + char *ptr; if ( u == NULL ) { return -1; @@ -639,19 +640,22 @@ desc2str( LDAPURLDesc *u, char *s, int len ) sep = 1; } - if ( !is_ipc && u->lud_host && strchr( u->lud_host, ':' )) { - is_v6 = 1; + if ( !is_ipc && u->lud_host && ( ptr = strchr( u->lud_host, ':' ))) { + if ( strchr( ptr+1, ':' )) + is_v6 = 1; } if ( u->lud_port ) { - len -= sprintf( s, "%s://%s%s%s:%d%n", u->lud_scheme, + sofar = sprintf( s, "%s://%s%s%s:%d", u->lud_scheme, is_v6 ? "[" : "", u->lud_host ? u->lud_host : "", is_v6 ? "]" : "", - u->lud_port, &sofar ); + u->lud_port ); + len -= sofar; } else { - len -= sprintf( s, "%s://%n", u->lud_scheme, &sofar ); + sofar = sprintf( s, "%s://", u->lud_scheme ); + len -= sofar; if ( u->lud_host && u->lud_host[0] ) { if ( is_v6 ) { s[sofar++] = '[';