[Libpst-commits] r58 - trunk

Joseph Nahmias jello at alioth.debian.org
Thu Dec 7 17:41:48 CET 2006


Author: jello
Date: 2006-12-07 17:41:47 +0100 (Thu, 07 Dec 2006)
New Revision: 58

Modified:
   trunk/readpst.c
Log:
rfc_2426_escape(): only realloc() when needed.
based on a patch from James Woodcock [closes tracker #303565].


Modified: trunk/readpst.c
===================================================================
--- trunk/readpst.c	2006-12-07 06:05:20 UTC (rev 57)
+++ trunk/readpst.c	2006-12-07 16:41:47 UTC (rev 58)
@@ -1396,6 +1396,7 @@
 // char *rfc2426_escape(char *str) {{{1
 char *rfc2426_escape(char *str) {
   static char *buf = NULL;
+  static int buflen = 0;
   char *a, *b;
   int x, y, z;
   DEBUG_ENT("rfc2426_escape");
@@ -1411,7 +1412,7 @@
     + chr_count(str, ';')
     + chr_count(str, '\n');
   z = chr_count(str, '\r');
-  x = strlen(str) + y - z;
+  x = strlen(str) + y - z + 1; // don't forget room for the NUL
 
 	if (y == 0 && z == 0) {
 		// there isn't any work required
@@ -1419,12 +1420,16 @@
 		return str;
 	}
 
-  buf = (char*) realloc(buf, x + 1); // don't forget room for the NUL
-	if ( buf == NULL )
-	{
-      fprintf(stderr, "Error: rfc2426_escape(): realloc(%d) returned NULL!\n", x + 1);
-      exit(1);
-	}
+  if ( x > buflen )  // if we need a bigger buffer than we have...
+  {
+    buflen = x;
+    buf = (char*) realloc(buf, buflen);
+	  if ( buf == NULL )
+	  {
+        fprintf(stderr, "Error: rfc2426_escape(): realloc(%d) returned NULL!\n", x + 1);
+        exit(1);
+	  }
+  }
 
   for ( a = str, b = buf; *a != '\0'; ++a, ++b )
 	  switch(*a) {




More information about the Libpst-commits mailing list