From: Gianni Tedesco Subject: Re: [PATCH]: xl: don't free string literals Date: Wed, 8 Sep 2010 13:13:10 +0100 Message-ID: <1283947990.20276.168.camel@qabil.uk.xensource.com> References: <1283775473.20276.135.camel@qabil.uk.xensource.com> <19590.32288.472147.509673@mariner.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <19590.32288.472147.509673@mariner.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Ian Jackson Cc: Xen Devel , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org On Tue, 2010-09-07 at 19:02 +0100, Ian Jackson wrote: > Gianni Tedesco writes ("[PATCH]: xl: don't free string literals"): > > The function init_dm_info() is initialising some strings from literals. > > This is bad juju because when the destructor is called we cannot know if > > the string literal was overridden with a strdup()'d value. Therefore > > strdup the initialisers in init_dm_info() and unconditionally free them > > before assigning non-default values to prevent their leakage. > > Can't we replace the dozen copies of this > > > - if (!xlu_cfg_get_string (config, "device_model", &buf)) > > + if (!xlu_cfg_get_string (config, "device_model", &buf)) { > > + free(dm_info->device_model); > > dm_info->device_model = strdup(buf); > > + } > > with something like > > > - if (!xlu_cfg_get_string (config, "device_model", &buf)) > > + if (!xlu_cfg_get_string_mallocd (config, "device_model", &buf)) { > > or whatever you want to call the function ? Or just change the > definition of xlu_cfg_get_string and declare that it always frees its > argument if it's not 0 to start with. > > Ian. Absolutely, I think I'll just change xlu_cfg_get_string() everywhere. Don't see a need to have two versions of a function and a non-obvious rule about which to use where. Will hack that up and re-send later. Gianni