[Ltrace-devel] [PATCH 1/6] Fix a memory leak in protolib_cache_maybe_load

Роман Донченко dpb at corrigendum.ru
Mon Apr 20 18:54:59 UTC 2015


Petr Machata <pmachata at redhat.com> писал в своём письме Mon, 20 Apr 2015  
11:08:12 +0300:

> Роман Донченко <dpb at corrigendum.ru> writes:
>> diff --git a/prototype.c b/prototype.c
>> index fa52ff3..22e54c4 100644
>> --- a/prototype.c
>> +++ b/prototype.c
>> @@ -547,8 +547,11 @@ protolib_cache_maybe_load(struct protolib_cache  
>> *cache,
>>  			  const char *key, int own_key, bool allow_private,
>>  			  struct protolib **retp)
>>  {
>> -	if (DICT_FIND_VAL(&cache->protolibs, &key, retp) == 0)
>> +	if (DICT_FIND_VAL(&cache->protolibs, &key, retp) == 0) {
>> +		if (*retp != NULL && own_key)
>> +			free((void *) key);
>>  		return 0;
>> +	}
>>
>>  	if (strdup_if(&key, key, !own_key) < 0) {
>>  		fprintf(stderr, "Couldn't cache %s: %s\n",
>
> This looks like the wrong fix.  The one who allocated the key should
> free it if protolib_cache_maybe_load returns a failure.

But it returns a success here.

In fact, I think we should take ownership of the key whenever the function  
returns zero, no matter what's in *retp. What do you think?



More information about the Ltrace-devel mailing list