On (26/02/14 13:00), Simo Sorce wrote:
On Wed, 2014-02-26 at 18:33 +0100, Lukas Slebodnik wrote:
> ehlo,
>
> Reported by: cppcheck
>
> void free_fun(struct info *info)
> free(info->name);
> free(info);
> info = NULL;
> ^^^^^^^^^^^
> Assignment to function parameter has no effect outside the function.
>
> Patch is attached.
Although the assignment is useless I do not like the solution.
I think the solution is not to remove the assignment but to change the
function prototype to take a pointer to the pointer we want to free so
that at the end of the function we can assign NULL to the original
variable we have been called to free.
Otherwise the caller has a dangling pointer.
So the solution for me is:
void free_fun(struct info **_info) {
struct info *info = *_info;
free(info->name);
free(info);
*info = NULL;
}
Simo.
I do not agree with you.
Free functions have parameter (void *) which will be freed.
e.g.:
void free(void *ptr);
int talloc_free (void *ptr)
/**
* Free a string allocated by a krb5 function.
*
* @param [in] context Library context
* @param [in] val String to be freed
*
* @version First introduced in 1.10
*/
void KRB5_CALLCONV
krb5_free_string(krb5_context context, char *val);
If you do not want to have dangling pointer, you need to assign NULL yourself
or use macro.
#define talloc_zfree(ptr) \
do { talloc_free(discard_const(ptr)); ptr = NULL; } while(0)
I do not want to introduce new "semantic" to free functions.
In many cases, you cannot change API of free function, because they are used
as a callback.
LS