Re: Someone stomping on our memory

From: Andres Kroonmaa <andre@dont-contact.us>
Date: Sat, 11 Nov 2000 01:28:34 +0200

please explain me some C stuff:

/* allocate a variable size buffer using best-fit pool */
memAllocBuf(size_t net_size, size_t * gross_size)
{
 [...]
    *gross_size = pool ? pool->obj_size : net_size;
  ...
stringInitBuf(String * s, size_t sz)
{
    s->buf = memAllocBuf(sz, &sz);
    assert(sz < 65536);
    s->size = sz;
}

stringLimitInit(String * s, const char *str, int len)
{
    assert(s && str);
    stringInitBuf(s, len + 1);
    s->len = len;
    xmemcpy(s->buf, str, len);
    s->buf[len] = '\0';
}

 So, memAllocBuf is modifying sz which is passed by pointer.
 memAllocBuf is called from stringInitBuf which gets sz from stack.
 stringInitBuf is called from stringLimitInit which passes len+1 for sz.

 I don't get how this should work.
 memAllocBuf is modifying something that is on the stack, and is produced
 runtime on the fly.

 Is it allowable to modify proc() params passed on stack? Is this reliable?
 What if &sz won't get modified? we would later memFree to a wrong pool?

stringClean(String * s)
{
    if (s->buf)
        memFreeBuf(s->size, s->buf);

 wondering 'cause latest crash had Medium Strings pool corrupted...

------------------------------------
 Andres Kroonmaa <andre@online.ee>
 Delfi Online
 Tel: 6501 731, Fax: 6501 708
 Pärnu mnt. 158, Tallinn,
 11317 Estonia
Received on Fri Nov 10 2000 - 16:31:50 MST

This archive was generated by hypermail pre-2.1.9 : Tue Dec 09 2003 - 16:12:57 MST