]> err.no Git - sope/blobdiff - sope-core/NGExtensions/FdExt.subproj/NSString+Escaping.m
fixed free bug
[sope] / sope-core / NGExtensions / FdExt.subproj / NSString+Escaping.m
index e31957e487beca7e9bf225f58b16e4b8c913bece..21f6b58872872dfd132c109beaa3525c80f9b628 100644 (file)
   // Unicode!
   unichar  *src;
   unichar  *buffer;
-  int      pos = 0;
+  int      len, pos, srcIdx;
   NSString *s;
   
-  if ((pos = [self length]) == 0)
+  if ((len = [self length]) == 0)
     return @"";
-
-  src = malloc(sizeof(unichar) * (pos + 2));
+  
+  src = malloc(sizeof(unichar) * (len + 2));
   [self getCharacters:src];
-  src[pos] = 0; // zero-terminate
+  src[len] = 0; // zero-terminate
   
-  buffer = malloc(sizeof(unichar) * ((pos * 2) + 1));
+  buffer = malloc(sizeof(unichar) * ((len * 2) + 1));
   
-  for (pos = 0; *src != 0; pos++, src++) {
-    switch (*src) {
+  for (pos = 0, srcIdx = 0; srcIdx < len; pos++, srcIdx++) {
+    switch (src[srcIdx]) {
       case '\n':
         buffer[pos] = '\\'; pos++;
         buffer[pos] = 'n';
         break;
         
       default:
-        buffer[pos] = *src;
+        buffer[pos] = src[srcIdx];
         break;
     }
   }
   buffer[pos] = '\0';
   
   s = [NSString stringWithCharacters:buffer length:pos];
-  free(buffer); buffer = NULL;
-  free(src);    src    = NULL;
+  
+  if (buffer != NULL) { free(buffer); buffer = NULL; }
+  if (src    != NULL) { free(src);    src    = NULL; }
   return s;
 }