[PATCH v2 08/12] gaih_inet: separate nss lookup loop into its own function

Siddhesh Poyarekar siddhesh@sourceware.org
Thu Mar 17 04:59:12 GMT 2022


On 17/03/2022 10:12, DJ Delorie wrote:
> Siddhesh Poyarekar via Libc-alpha <libc-alpha@sourceware.org> writes:
>> ---
> 
> One memory leak needs fixing...
> 
>> +static void
>> +gaih_result_reset (struct gaih_result *res)
>> +{
>> +  if (res->free_at)
>> +    free (res->at);
>> +  free (res->canon);
>> +  memset (res, 0, sizeof (*res));
>> +}
> 
> (I think this fixes the problem in [07/12] ;)

Hah, this is why my first though on your comment on 7/12 was "what do 
you mean?  I memset the whole block" :)  I'll move this bit into 7/12.

>> -/* Convert struct hostent to a list of struct gaih_addrtuple objects.
>> -   h_name is not copied, and the struct hostent object must not be
>> -   deallocated prematurely.  *RESULT must be NULL or a pointer to a
>> -   linked-list.  The new addresses are appended at the end.  */
>> +/* Convert struct hostent to a list of struct gaih_addrtuple objects.  h_name
>> +   is not copied, and the struct hostent object must not be deallocated
>> +   prematurely.  *RESULT must be NULL or a pointer to a linked-list.  The new
>> +   addresses are appended at the end.  */
> 
> no changes, ok.
> 
>>   static bool
>> -convert_hostent_to_gaih_addrtuple (const struct addrinfo *req,
>> -				   int family,
>> -				   struct hostent *h,
>> -				   struct gaih_addrtuple **result)
>> +convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, int family,
>> +				   struct hostent *h, struct gaih_result *res)
> 
> Ok.
> 
>> -  struct gaih_addrtuple *array = *result;
>> +  struct gaih_addrtuple *array = res->at;
> 
> Ok.
> 
>> -  array = realloc (*result, (old + count) * sizeof (*array));
>> +  array = res->at = realloc (res->at, (old + count) * sizeof (*array));
>>   
>>     if (array == NULL)
>>       return false;
>>   
>> -  *result = array;
> 
> This is not the same - if realloc fails, the original res->at is lost
> and can no longer be free'd.
> 

Oops, indeed.  I'll fix this.

Thanks,
Siddhesh


More information about the Libc-alpha mailing list