Re: Buffer Overflow

From: Absolute Newbie (absnewbie@hotmail.com)
Date: 04/06/03


From: absnewbie@hotmail.com (Absolute Newbie)
Date: 5 Apr 2003 17:20:47 -0800

Juha Laiho <Juha.Laiho@iki.fi> wrote:
-------------------------------------
> I've been bitten by this - and there is a need to zero the buffer you get
> from malloc(): while the OS memory allocation routines will clear (to
> some value; I think the actual value hasn't been specified) the memory
> areas they're giving to your process, malloc() may also return memory
> that was already used by your process, but free()'d (but still not yet
> returned to the OS). And in this case the contents will not be scrubbed;
> the memory will contain whatever it did contain when your program freed
> the area.

A simple way to avoid unpredicted results is to always use calloc()
instead of malloc(). calloc() will zero out the buffer upon
allocation:

char *buffer = calloc(1024, 1);

instead of:

char *buffer = malloc(1024);
memset(buffer, 0, 1024); /* you could forget this one :) */

regards,

absnewbie



Relevant Pages

  • Re: C 99 compiler access
    ... > nm> reliably allocating some piece of memory. ... We're not talking about implementing malloc in portable C - it's part of the ... concern is that library implementations are not implementing the required ... using malloc to allocate the buffer memory. ...
    (comp.lang.c)
  • Re: Benchmark: STLs list vs. hand-coded one
    ... if you bother to check the pointer from malloc or use newand throw ... buffer to 640MBytes. ... To support your efficient memory management you limit symbol size to ... Yes you could require the compiler to jump ...
    (comp.arch.embedded)
  • Re: NTFS - Kernel memory leak in driver for kernel 2.4.28?
    ... I should say that the malloc() succeeds, but the 16mb I need for the ... buffer are not available. ... memory tied up in the inode and dentry cache. ... kernel attempts to use for the dentry/inode cache, or make it much, ...
    (Linux-Kernel)
  • Re: style question,itoa
    ... sensible to have the caller pass in a buffer. ... technically useless malloc and free calls, ... priori when memory allocations would fail. ...
    (comp.unix.programmer)
  • Re: Segmentation fault in mallopt/malloc call
    ... > overflows or buffer underflows, or some other random data corruption. ... > using a memory debugger (e.g. electric fence, ... > a bug in malloc(), ...
    (comp.lang.c)