[Snort-devel] Bug : misuse of calloc (patch included)

Erik de Castro Lopo erikd+snort at ...2292...
Thu Jul 29 15:51:02 EDT 2004


On Thu, 29 Jul 2004 08:47:47 +0200
"Gisle Vanem" <giva at ...2432...> wrote:

> Hm, I've always thought it was the responsibility of the compiler
> and person using it, to care about alignment. How can calloc() know
> the layout of what struct you're asking for? Besides, on those problematic
> platforms, wouldn't every allocation be a multiple of ALIGN (normally 8)?
> 
> >  void * mwmNew()
> >  {
> > -   MWM_STRUCT * p = (MWM_STRUCT * )calloc( sizeof(MWM_STRUCT),1 );
> > +   MWM_STRUCT * p = (MWM_STRUCT * )calloc( 1, sizeof(MWM_STRUCT) );
> 
> Isn't this much simpler:
>   MWM_STRUCT * p = calloc(sizeof(*p), 1);

This is still wrong, the second argument is the size of a single 
element that you are allocating and the first is the count.

> And why cast a 'void *' from calloc()? This is C, not C++ right?

I agree. My patch was supposed to be the minimal patch that would
correct the problem I identified.

Erik
-- 
------------------------------------------------------
[N] Erik de Castro Lopo, Senior Computer Engineer
[E] erik.de.castro.lopo at ...2292...
[W] http://www.sensorynetworks.com
[T] +61 2 83022726 
[F] +61 2 94750316 
[A] L4/140 William St, East Sydney NSW 2011, Australia
------------------------------------------------------
A good debugger is no substitute for a good test suite.




More information about the Snort-devel mailing list