Re: Buffer Overflow
From: Bjorn Reese (breese@mail1.stofanet.dk)
Date: 04/06/03
- Next message: Bjorn Reese: "Re: Buffer Overflow"
- Previous message: Bjorn Reese: "Re: A new and very robust method for doing file locking over NFS?"
- In reply to: Juha Laiho: "Re: Buffer Overflow"
- Next in thread: Goran Larsson: "Re: Buffer Overflow"
- Reply: Goran Larsson: "Re: Buffer Overflow"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Sun, 06 Apr 2003 09:45:30 +0000 From: Bjorn Reese <breese@mail1.stofanet.dk>
Juha Laiho 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
C99 says that the values will be "indeterminate", so you cannot assume
that all entries in the buffer has the same value.
> 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.
I think Alan's point was that since the allocated buffer will be written
with data obtained from the socket before the data is read from the buffer,
then there is no need to zero the buffer first. This is equivalent to the
following sequence:
int a = 0; /* Make sure that the value is initialized */
a = 42; /* Assign the real content to the value */
If there is a possibility that you will access the variable between the
two assignments, then it is a good practice to initialize it to zero (or
some other value). However, if not, then you are just doing superfluous
work (which could turn out to be a performance hog in the case of memset
on large buffers.)
- Next message: Bjorn Reese: "Re: Buffer Overflow"
- Previous message: Bjorn Reese: "Re: A new and very robust method for doing file locking over NFS?"
- In reply to: Juha Laiho: "Re: Buffer Overflow"
- Next in thread: Goran Larsson: "Re: Buffer Overflow"
- Reply: Goran Larsson: "Re: Buffer Overflow"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|