diff src/pst2ldif.cpp @ 38:f5c024aa1dc5 stable-0-5-9

more valgrind fixes
author carl
date Sun, 12 Aug 2007 14:30:15 -0700
parents 12cac756bc05
children 183ae993b9ad
line wrap: on
line diff
--- a/src/pst2ldif.cpp	Fri Aug 10 08:06:13 2007 -0700
+++ b/src/pst2ldif.cpp	Sun Aug 12 14:30:15 2007 -0700
@@ -556,25 +556,26 @@
 	else {
 
 		// calculate space required to escape all the following characters
-		x = strlen(str) +(y=(chr_count(str, ',')*2) + (chr_count(str, '\\')*2) + (chr_count(str, ';')*2) + (chr_count(str, '\n')*2));
+		y = chr_count(str, '\\')
+		  + chr_count(str, ';');
 		z = chr_count(str, '\r');
 		if (y == 0 && z == 0)
 			// there isn't any extra space required
 			ret = str;
 		else {
-			buf = (char*) realloc(buf, x+1);
+			x = strlen(str) + y - z + 1; // don't forget room for the NUL
+			buf = (char*) realloc(buf, x);
 			a = str;
 			b = buf;
 			while (*a != '\0') {
 				switch(*a) {
-				 // case ',' :
 					case '\\':
 					case ';' :
-				 // case '\n':
 						*(b++)='\\';
 						*b=*a;
 					break;
-					case '\r':
+					case '\r':  // skip cr
+						b--;
 						break;
 					default:
 						*b=*a;
@@ -582,7 +583,7 @@
 				b++;
 				a++;
 			}
-			*b = '\0';
+			*b = '\0'; // NUL-terminate the string (buf)
 			ret = buf;
 		}
 	}