diff --git a/libraries/libldif/fetch.c b/libraries/libldif/fetch.c
index 545253e80830ae7da55b881b1117850f847c982b..36baf734c2fa3ee9f92219998ba157d896be4c62 100644
--- a/libraries/libldif/fetch.c
+++ b/libraries/libldif/fetch.c
@@ -74,7 +74,7 @@ ldif_fetch_url(
 	total = 0;
 
 	while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
-		char *newp = ber_memrealloc( p, total + bytes );
+		char *newp = ber_memrealloc( p, total + bytes + 1 );
 		if( newp == NULL ) {
 			ber_memfree( p );
 			fclose( url );
@@ -96,6 +96,7 @@ ldif_fetch_url(
 		p = newp;
 	}
 
+	p[total] = '\0';
 	*valuep = p;
 	*vlenp = total;