[lxc-devel] strdup return value

Daniel Lezcano daniel.lezcano at free.fr
Wed Nov 18 15:41:28 UTC 2009


Michael Tokarev wrote:
> Andrian Nord wrote:
>   
>> On Wed, Nov 18, 2009 at 02:25:20AM +0300, Michael Tokarev wrote:
>>     
>>> I wodner why all code checks for malloc return value
>>> but almost no places checks for strdup.
>>>       
>> Because this is an error? Probably it should be checked everywhere it
>> does not.
>>
>>     
>>> Maybe it is better idea to define xmalloc()/xstrdup()
>>> that aborts if memory allocation failed, instead of
>>>       
>          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   
>>> scattering error checking all over?
>>>       
>> You mean something like that:
>>
>> char *src = "blabla";
>> char *dest;
>>
>> if (xstrdup(&dest, src)) {
>> 	SYSERROR("failed to allocate memory");
>> 	return -1;
>> }
>>     
>
> I mean something like this:
>
>   char *xstrdup(const char *src) {
>     char *dest = strdup(src);
>     if (!dest) {
>        SYSERROR("failed to allocate memory");
>        abort();
>     }
>     return dest;
>   }
>
> as used in countless other projects all around the world.
>   
I suppose memory allocation failure is the worst case we can found in a 
program and this can be considered as fatal but
what about if there is some cleanup to do in case of failure ?





More information about the lxc-devel mailing list