[lxc-devel] strdup return value

Andrian Nord nightnord at gmail.com
Tue Nov 17 23:50:36 UTC 2009


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;
}

instead of

char *src = "blabla";
char *dest;

if (!(dest = strdup(src))) {
	SYSERROR("failed to allocate memory");
	return -1;
}

or, more readable version,
...

dest = strdup(src);

if (!dest) {
...

?

Is it really matters?

Failing just from program instead of returning some 'bad value' is not
good, it's not always appliable - sometimes failture to duplicate string
could be not fatal for the whole process.

Anyway, even it's fatal we should deinitialize propery, to sure
that all file handlers are closed etc. Also this will break callback
errors chain.




More information about the lxc-devel mailing list