[torqueusers] A question on the attrlist_alloc function in attr_func.c

Garrick Staples garrick at usc.edu
Sat Jul 16 11:04:53 MDT 2005


On Sat, Jul 16, 2005 at 11:16:38AM -0400, Prakash Velayutham alleged:
> Hi,
> 
> I am going through torque source code for some reason and I
> found this. I am not clear how this works. Can someone explain?
> 
> The attrlist_alloc function in attr_func.c file has this body.
> 
> svrattrl *attrlist_alloc(szname, szresc, szval)
>         int szname;
>         int szresc;
>         int szval;
> {
>         register size_t tsize;
>         svrattrl *pal;
> 
>         tsize = sizeof(svrattrl) + szname + szresc + szval;
>         pal = (svrattrl *)malloc(tsize);
>         if (pal == (svrattrl *)0)
>                 return ((svrattrl *)0);
> #ifdef DEBUG
>         memset(pal, 0, sizeof(svrattrl));
> #endif
> 
>         CLEAR_LINK(pal->al_link);       /* clear link */
>         pal->al_atopl.next = 0;
>         pal->al_tsize = tsize;          /* set various string
> sizes */
>         pal->al_nameln = szname;
>         pal->al_rescln = szresc;
>         pal->al_valln  = szval;
>         pal->al_flags  = 0;
>         pal->al_op     = SET;
>         pal->al_name = (char *)pal + sizeof (svrattrl);
>         if (szresc)
>                 pal->al_resc = pal->al_name + szname;
>         else
>                 pal->al_resc = (char *)0;
>         pal->al_value = pal->al_name + szname + szresc;
>         return (pal);
> }
> 
> The memset call is setting 52 bytes of memory area pointed to
> by pal to 0. But pal is initially allocated only 4 bytes using
> malloc. Isn't this overstepping memory?

Looks to me like it's not stepping far enough.  We malloc the 52 bytes and the
3 sizes that are passed in, and we only memset the 52 bytes.

And of course, all those casts of malloc and 0 need to go away.

-- 
Garrick Staples, Linux/HPCC Administrator
University of Southern California
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.supercluster.org/pipermail/torqueusers/attachments/20050716/8294ca7f/attachment.bin


More information about the torqueusers mailing list