aboutsummaryrefslogtreecommitdiff
path: root/lib/memdebug.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2002-02-28 12:18:15 +0000
committerDaniel Stenberg <daniel@haxx.se>2002-02-28 12:18:15 +0000
commit0cacbc892cb73b3903805541470d7826094d934e (patch)
tree1c39cb54a9691c9518fe9f7a2cbe3eea039c9a69 /lib/memdebug.c
parent6753c3c71552ebab50390209211d26c9a3e02109 (diff)
always allocates at least 64 bytes for real, and damages them before free
Diffstat (limited to 'lib/memdebug.c')
-rw-r--r--lib/memdebug.c38
1 files changed, 30 insertions, 8 deletions
diff --git a/lib/memdebug.c b/lib/memdebug.c
index 66fe60136..42e454b72 100644
--- a/lib/memdebug.c
+++ b/lib/memdebug.c
@@ -47,6 +47,11 @@
/* DONT include memdebug.h here! */
+struct memdebug {
+ int size;
+ char mem[1];
+};
+
/*
* Note that these debug functions are very simple and they are meant to
* remain so. For advanced analysis, record a log file and write perl scripts
@@ -67,15 +72,21 @@ void curl_memdebug(const char *logname)
}
-void *curl_domalloc(size_t size, int line, const char *source)
+void *curl_domalloc(size_t wantedsize, int line, const char *source)
{
- void *mem=(malloc)(size);
+ void *mem;
+ size_t size;
+
+ /* alloc at least 64 bytes */
+ size = wantedsize>64?wantedsize:64;
+
+ mem=(malloc)(size);
if(mem)
/* fill memory with junk */
memset(mem, 0xA5, size);
- if(logfile)
+ if(logfile && source)
fprintf(logfile, "MEM %s:%d malloc(%d) = %p\n",
- source, line, size, mem);
+ source, line, wantedsize, mem);
return mem;
}
@@ -90,20 +101,28 @@ char *curl_dostrdup(const char *str, int line, const char *source)
exit(2);
}
- mem=(strdup)(str);
len=strlen(str)+1;
+
+ mem=curl_domalloc(len, 0, NULL); /* NULL prevents logging */
+ memcpy(mem, str, len);
+
if(logfile)
fprintf(logfile, "MEM %s:%d strdup(%p) (%d) = %p\n",
source, line, str, len, mem);
return mem;
}
-void *curl_dorealloc(void *ptr, size_t size, int line, const char *source)
+void *curl_dorealloc(void *ptr, size_t wantedsize,
+ int line, const char *source)
{
- void *mem=(realloc)(ptr, size);
+ void *mem;
+
+ size_t size = wantedsize>64?wantedsize:64;
+
+ mem=(realloc)(ptr, size);
if(logfile)
fprintf(logfile, "MEM %s:%d realloc(%p, %d) = %p\n",
- source, line, ptr, size, mem);
+ source, line, ptr, wantedsize, mem);
return mem;
}
@@ -114,7 +133,10 @@ void curl_dofree(void *ptr, int line, const char *source)
source, line);
exit(2);
}
+ /* we know this is least 64 bytes, destroy this much */
+ memset(ptr, 0x13, 64);
+ /* free for real */
(free)(ptr);
if(logfile)