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

Gisle Vanem giva at ...2432...
Wed Jul 28 23:48:07 EDT 2004


"Erik de Castro Lopo" said:

> which is asking for a pointer to sizeof(SOME_STRUCT) elements
> of size one. As such, it is legal for the allocator to return
> a pointer without worrying about alignment at all. Then, if
> the struct contains say an int32_t, the int32_t may not be 
> aligned to a four byte boundary which on some architectures 
> can cause a bus fault.

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);

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

--gv






More information about the Snort-devel mailing list