[or-cvs] Never call free() on tor_malloc()d memory. This is unlikely...

Nick Mathewson nickm at seul.org
Fri Sep 30 20:48:01 UTC 2005


Update of /home/or/cvsroot/tor/src/or
In directory moria:/tmp/cvs-serv4911/src/or

Modified Files:
	buffers.c main.c test.c 
Log Message:
Never call free() on tor_malloc()d memory. This is unlikely to be our current leak, but it may help dmalloc work.

Index: buffers.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/buffers.c,v
retrieving revision 1.171
retrieving revision 1.172
diff -u -d -r1.171 -r1.172
--- buffers.c	30 Sep 2005 01:09:52 -0000	1.171
+++ buffers.c	30 Sep 2005 20:47:58 -0000	1.172
@@ -83,14 +83,15 @@
   if (buf->cur + buf->datalen <= buf->mem+buf->len) {
     return;
   } else {
-    char *newmem;
+    char *newmem, *oldmem;
     size_t sz = (buf->mem+buf->len)-buf->cur;
     log_fn(LOG_WARN, "Unexpected non-normalized buffer.");
     newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(buf->len)));
     SET_GUARDS(newmem, buf->len);
     memcpy(newmem, buf->cur, sz);
     memcpy(newmem+sz, buf->mem, buf->datalen-sz);
-    free(RAW_MEM(buf->mem));
+    oldmem = RAW_MEM(buf->mem);
+    tor_free(oldmem); /* Can't use tor_free directly. */
     buf->mem = buf->cur = newmem;
     check();
   }
@@ -196,11 +197,11 @@
       !buf->datalen &&
       buf->len >= 1<<16) {
     /* don't realloc; free and malloc */
-    char *newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(new_capacity)));
+    char *oldmem, *newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(new_capacity)));
     SET_GUARDS(newmem, new_capacity);
-    free(RAW_MEM(buf->mem));
+    oldmem = RAW_MEM(buf->mem);
+    tor_free(oldmem);
     buf->mem = buf->cur = newmem;
-
   } else {
     buf->mem = GUARDED_MEM(tor_realloc(RAW_MEM(buf->mem),
                                        ALLOC_LEN(new_capacity)));
@@ -371,9 +372,11 @@
 void
 buf_free(buf_t *buf)
 {
+  char *oldmem;
   assert_buf_ok(buf);
   buf->magic = 0xDEADBEEF;
-  free(RAW_MEM(buf->mem));
+  oldmem = RAW_MEM(buf->mem);
+  tor_free(oldmem);
   buf_total_alloc -= buf->len;
   buf_total_used -= buf->datalen;
   tor_free(buf);

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/main.c,v
retrieving revision 1.561
retrieving revision 1.562
diff -u -d -r1.561 -r1.562
--- main.c	30 Sep 2005 01:09:52 -0000	1.561
+++ main.c	30 Sep 2005 20:47:58 -0000	1.562
@@ -1464,7 +1464,7 @@
   path_to_torrc = tor_malloc(len);
   if (tor_snprintf(path_to_torrc, len, "%s%s%s", szDrive,  szDir, torrc)<0) {
     printf("Failed: tor_snprinf()\n");
-    free(path_to_torrc);
+    tor_free(path_to_torrc);
     return 0;
   }
 
@@ -1772,7 +1772,7 @@
   }
 
   if ((hSCManager = nt_service_open_scm()) == NULL) {
-    free(command);
+    tor_free(command);
     return 0;
   }
 
@@ -1789,7 +1789,7 @@
     printf("CreateService() failed : %s\n", errmsg);
     CloseServiceHandle(hSCManager);
     LocalFree(errmsg);
-    free(command);
+    tor_free(command);
     return 0;
   }
 

Index: test.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/test.c,v
retrieving revision 1.206
retrieving revision 1.207
diff -u -d -r1.206 -r1.207
--- test.c	29 Sep 2005 06:45:03 -0000	1.206
+++ test.c	30 Sep 2005 20:47:58 -0000	1.207
@@ -543,9 +543,9 @@
   test_eq(i,0);
   test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
 
-  free(data1);
-  free(data2);
-  free(data3);
+  tor_free(data1);
+  tor_free(data2);
+  tor_free(data3);
 }
 
 static void



More information about the tor-commits mailing list