I have this in my personal standard C library in my "STR" module :
/* ---------------------------------------------------------------------
STR_safecopy()
There is no word "outbonding". You probably mean "overflow". You may
have been trying to make a verb from "out of bounds", but first, bound
!= bond, and second, bound in this sense is a transitive verb not an
intransitive one and so doesn't extend (conjugate?) that way.
---------------------------------------------------------------------
I: destination address
I: destination size
I: source address
O: destination address
--------------------------------------------------------------------- */
char *STR_safecopy (char *const des
,size_t const size
,char const *const src)
{
char *s_out = NULL;
if (des && size && src)
{
memcpy (des, src, size - 1);
des[size - 1] = 0;
s_out = des;
}
This is unsafe if the buffer pointed to by src is smaller than that
pointed to by des, or more accurately than size which presumably must
be <= the size pointed to by des, because you read memory not
allocated to this object, and possibly unallocated (to you) and even
nonexistent. And even if that doesn't happen, it is inefficient,
possibly very much so. You should use the minimum of the (current)
length at src or the usable size (i.e. -1) at des.
PS- I (would) use dst instead of des; des could be the abbreviation of
quite a few other things I might mean as a parameter, like descriptor,
design, desire(d), not to mention cryptographic DES.
- David.Thompson1 at worldnet.att.net