From: Kenneth J. Davis (jeremyd_at_ctc.net)
Date: Sun Apr 18 2004 - 20:42:24 EDT
Why do we need both ut_clonestring and ut_strdup?
Are there really any cases where not cloning an
empty string ("") is needed? [note that in at
least one place using it caused a crash as it
left a string NULL instead of ""] The only
other difference functionally that I see is the
ut_strdup also clears the memory region, so
I'm not sure if there is a performance issue
here, but in that case we should evaluate if
ut_strdup really needs to do it.
As both functions allocate via malloc the memory,
I don't see a problem using memcpy instead of
memmove, as the memory regions should not overlap.
Below are the functions for your reference.
Personally, I'm in favor of removing UT_clonestring,
and replacing usage with UT_strdup. If there are
any places that should not clone an empty string,
then that code can do so explicitly. Any objections?
I haven't grep'd the code for all uses, so maybe there
are obvious cases, it's just the 2 functions seem too
easily confused leading to other potential unexpected
NULL pointers.
Thanks,
Jeremy
char * UT_strdup(const char * szSource)
{
UT_return_val_if_fail(szSource, NULL);
int len = strlen(szSource)+1;
if(char * ret = static_cast<char *>(UT_calloc(len, sizeof(char))))
return(static_cast<char *>(memcpy(ret, szSource, len)));
else
return(NULL);
}
bool UT_cloneString(char *& rszDest, const char * szSource)
{
if (szSource && *szSource)
{
UT_uint32 length = strlen(szSource) + 1;
rszDest = static_cast<char *>(malloc(length));
if (!rszDest)
return false;
memmove(rszDest, szSource, length);
}
else
rszDest = NULL;
return true;
}
This archive was generated by hypermail 2.1.4 : Sun Apr 18 2004 - 20:47:27 EDT