summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGerald Carter <jerry@samba.org>2000-08-12 14:14:40 +0000
committerGerald Carter <jerry@samba.org>2000-08-12 14:14:40 +0000
commit95496b974a2920369874c1cf29985723f6fc3058 (patch)
treefebbc2ba256a054b6130b6f2485b8c68ed2fec32
parent50cef4fa0d5ee9ac9ae6d5f41f88320bf02e9fb8 (diff)
downloadsamba-95496b974a2920369874c1cf29985723f6fc3058.tar.gz
samba-95496b974a2920369874c1cf29985723f6fc3058.tar.bz2
samba-95496b974a2920369874c1cf29985723f6fc3058.zip
reworked talloc() to in order to help track down invalid
memory access. Tridge, I don't think using 0xdeadbeef for size allocations of 0 is going to work. I ended up having to use NULL as much code that works on UNISTR checks to see if the buffer ptr is NULL. So valid code ends up with a seg fault. Rather than rewriting it all, I added a DEBUG_TALLOC #ifdef in talloc.h that sets a macro BAD_PTR. This is the value assigned to ptr for an allocation of 0 bytes. jerry (This used to be commit 233b9cffa2350552d9f775f791d5d5e0464a1ed4)
-rw-r--r--source3/include/talloc.h6
-rw-r--r--source3/lib/talloc.c51
2 files changed, 32 insertions, 25 deletions
diff --git a/source3/include/talloc.h b/source3/include/talloc.h
index a0dc3d9795..85a722d080 100644
--- a/source3/include/talloc.h
+++ b/source3/include/talloc.h
@@ -21,6 +21,12 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
+#ifdef DEBUG_TALLOC
+#define BAD_PTR (void*)0xdeadbeef
+#else
+#define BAD_PTR NULL
+#endif
+
struct talloc_chunk {
struct talloc_chunk *next;
void *ptr;
diff --git a/source3/lib/talloc.c b/source3/lib/talloc.c
index 577f761072..acc6204bed 100644
--- a/source3/lib/talloc.c
+++ b/source3/lib/talloc.c
@@ -55,37 +55,38 @@ TALLOC_CTX *talloc_init(void)
void *talloc(TALLOC_CTX *t, size_t size)
{
void *p;
-
if (size == 0)
{
/* debugging value used to track down
- memory problems */
- p = (void*)0xdeadbeef;
+ memory problems. BAD_PTR is defined
+ in talloc.h */
+ p = BAD_PTR;
+ return p;
}
- else
- {
- size = (size + (TALLOC_ALIGN-1)) & ~(TALLOC_ALIGN-1);
-
- if (!t->list || (t->list->total_size - t->list->alloc_size) < size) {
- struct talloc_chunk *c;
- size_t asize = (size + (TALLOC_CHUNK_SIZE-1)) & ~(TALLOC_CHUNK_SIZE-1);
-
- c = (struct talloc_chunk *)malloc(sizeof(*c));
- if (!c) return NULL;
- c->next = t->list;
- c->ptr = (void *)malloc(asize);
- if (!c->ptr) {
- free(c);
- return NULL;
- }
- c->alloc_size = 0;
- c->total_size = asize;
- t->list = c;
- }
- p = ((char *)t->list->ptr) + t->list->alloc_size;
- t->list->alloc_size += size;
+ /* normal code path */
+ size = (size + (TALLOC_ALIGN-1)) & ~(TALLOC_ALIGN-1);
+
+ if (!t->list || (t->list->total_size - t->list->alloc_size) < size) {
+ struct talloc_chunk *c;
+ size_t asize = (size + (TALLOC_CHUNK_SIZE-1)) & ~(TALLOC_CHUNK_SIZE-1);
+
+ c = (struct talloc_chunk *)malloc(sizeof(*c));
+ if (!c) return NULL;
+ c->next = t->list;
+ c->ptr = (void *)malloc(asize);
+ if (!c->ptr) {
+ free(c);
+ return NULL;
+ }
+ c->alloc_size = 0;
+ c->total_size = asize;
+ t->list = c;
}
+
+ p = ((char *)t->list->ptr) + t->list->alloc_size;
+ t->list->alloc_size += size;
+
return p;
}