scrīpsit Philipp Takacs <[email protected]>: > [2016-03-18 21:12] [email protected] > > 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. > > > 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. > > My idea was something like: > > char *mh_xstrdup(char *s, boolean exit_on_failure) > > But I reconsider this is a bad idea. It's better to use strdup() > for the libary and mh_xstrdup() for the programms. In the first place > you don't need to add return-value check. > > For your patch this means: > 1) use adios in mh_xstrdup() > 2) use mh_xstrdup() to replace getcpy()/strdup(), if the return value isn't c > hecked > 3) use strdup() to replace getcpy(), if the return value is checked
OK. I understand better now. I'll work on this. > And some notice to your example implementation > > > 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. > > In general this is this[0] a good idea to handle error returns, but > for errno this is unnecessary, because the caller can also check errno. - > [0]: In most cases it's the other way around. The return type > is the error indicator and the actual return is a pointer argument. Thanks for your explanation. I thought that one needed to save errno, because it may be changed by other library calls [*]; that's why I pointed the variable "errsv" at the value of errno in my example. But I see now that this is unnecessary in my example, where no library calls are made after errno is possibly set by strdup(). Michael [*]: errno(3)
