Re: Unsafe C++ memory allocation with new operator

From: Nick Lewycky <nicholas@dont-contact.us>
Date: Sat, 08 Jan 2005 14:09:57 -0500

Henrik Nordstrom wrote:
> On Sat, 8 Jan 2005, Serassio Guido wrote:
>
>> On Linux this change works fine, but on Windows, and may be on other
>> platforms, Squid crashes when parsing squid.conf because the memory
>> used is not set to zero like on Linux.
>
>
> xmalloc does not clear memory on Linux either.
>
>> From my understanding of C++ the new operator is not supposed to
>> guarantee
>
> the memory is cleared. This is the job of the constructor and related
> member initializers, where the default constructor memsets the object to 0.
>
> But it's possible this has changed since I studied C++ years ago..

You have it right, but if you look at Robert's patch, he turned calls of
"xcalloc" into operator new. That's fine, but he also needs to modify
the class being allocated, RemovalPolicySettings, so that its
constructor will zero whatever it needs.

Here's a one-line fix to patch it up, but the constructor probably ought
to belong a cc file instead of the header.

Developers making these sorts of changes might want to test them with a
tool like Valgrind from valgrind.kde.org (Linux-specific, sorry) which
will detect dependent reads to uninitialized memory.

Nick Lewycky

Received on Sat Jan 08 2005 - 12:10:07 MST

This archive was generated by hypermail pre-2.1.9 : Tue Feb 01 2005 - 12:00:02 MST