This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Using strlcpy if target OS allows it
Joe Buck <Joe.Buck@synopsys.COM> writes:
> There appear to be only four calls of strcpy in the library itself
> (as opposed to the testsuite). All four take this exact form:
>
> char* __sav = new char[std::strlen(__old) + 1];
> std::strcpy(__sav, __old);
>
> Perhaps the V3 maintainers could be persuaded to accept a patch that
> replaces all four with a call to
>
> inline char* __cstring_duplicate(const char* __old) {
> unsigned __size = std::strlen(__old) + 1;
> char* __sav = new char[__size];
> return std::strcpy(__sav, __old);
> }
>
> This would have maintainance advantages, as four identical code sequences
> become one.
If a change is made, why not replace the strcpy() with a memcpy()? As
it is, strcpy() is copying until it reaches the terminating null char,
even though we already know how many bytes to copy. memcpy() is often
more efficent than strcpy as well, even those implementations that go
to Herculean efforts to do only word-aligned read/writes.
--jtc
--
J.T. Conklin