[torqueusers] A question on the attrlist_alloc function inattr_func.c

David B Jackson jacksond at clusterresources.com
Sat Jul 16 13:04:53 MDT 2005


Garrick,

  Looking at the calling routines, everything is properly handled. 
However, the latest TORQUE code has already been changed to do a calloc
instead of a malloc to effectively clear the entire buffer.

Dave


> 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
> _______________________________________________
> torqueusers mailing list
> torqueusers at supercluster.org
> http://www.supercluster.org/mailman/listinfo/torqueusers
>



More information about the torqueusers mailing list