scrÄ«psit Philipp Takacs <[email protected]>:

> [2016-03-16 09:05] [email protected]
> > One item in the list found in docs/TODO was replace getcpy() with
> > strdup().  So I've added a subroutine to sbr/utils.c called "mh_xstrdup"
> > with which I've replaced all calls to getcpy() and to strdup(), except
> > those in sbr/mf.c; the file sbr/mf.c defines its own getcpy(), so I've
> > left it alone.  See the attached patch.
> 
> Thanks for your patch. But your patch doesn't cover all the problems.

Hi.  Thanks for your detailed response.

> getcpy() internal use mh_xcalloc, which calls exit() if it fail, but
> strdup and mh_xstrdup does not. The problem is in the libary[0] (sbr-dir)
> it's a bad idea to have a function that exit the hole programm.
-
> [0]: I know there are a bunch of getcpy/mh_xcalloc/adios calls in the
> libary, but we shouldn't add more.
-
> Maybe you add an boolean argument to mh_xstrdump() to choos, if the
> caller checks the return value or not.

OK, mh_xstrdup() needs improvement.  I understand that mh_xstrdup()
shouldn't call adios() instead of advise().  But I don't understand how
mh_xstrdup() should behave if strdup() returns NULL due to insufficient
available memory.  Should mh_xstrdup() indicate quietly to the caller
that strdup() failed?  For example, a function

        char *
        mh_xstrdup(char * s, int * errsv)

could set the value of errsv to the value of errno from errno.h after
calling strdup().  This way, the caller of mh_xstrdup() could choose
whether to check for an error.  See the code attached to this message.

Michael
#include <errno.h>
#include <string.h>

#define _XOPEN_SOURCE 500

char *
mh_xstrdup(char * s, int * errsv)
{
	char * tmp;
	errno = 0;
	tmp = strdup(s ? s : "");
	*errsv = errno;
	return tmp;
}

Reply via email to