On Tue, 2011-08-02 at 17:26 +0200, Miroslav Lichvar wrote:
On Tue, Aug 02, 2011 at 04:58:19PM +0200, Denys Vlasenko wrote:
> I don't see it. Let me walk it through:
>
> char *msg = xasprintf("%s", result);
> ^^^^^^^^^^^^^ we allocated new string, msg
> res = new_report_result(REPORT_RESULT_TYPE_MESSAGE, msg);
> ^^^^^^^^^^^^^ we use it to build res
new_report_result accepts char *, msg is now owned by the struct
Thanks, now I see.
> msg = format_report_result(res);
> ^^^^^^^^^^^^^ we create final message string from res
> and assign it to msg. Here we lost old msg pointer
> during assignment. It's leaked.
free_report_result(res);
The original msg is now freed.
If you think new_report_result should accept const char * instead, we
can add the extra strdup+free calls.
I don't know what is better. Taking ownership is more efficient
in some cases; but it's more confusing.
--
vda