Tom
Wed Feb 08 13:26:05 CST 2006
Hi DR ,
You could already use strcpy_s() this same way and the only difference would
be that it wouldn't put a numeric 0 in your last count item. However, it
would safely know the size of the buffer in many occasions:
TCHAR myBuff[20];
_tcscpy_s(myBuff,_T("Test Data"));
or
_tcscpy_s(myBuff, _countof(MyBuff), _T("Test Data"));
Both are safe because of the template implementation. IOW, there is no
longer any really good reason to implement your own version and even if you
do the new compiler will gripe about how strcpy() is deprecated so you'll
have to put:
#pragma warning (disable : 4996)
all over in your code :o)
Tom
"DR" <dr1234@yahoo.com> wrote in message
news:e$P0BDNLGHA.604@TK2MSFTNGP14.phx.gbl...
> This function should be save? What do you think?
>
> LPCSTR my_strncpy(LPSTR a, LPCSTR b, int cnt)
> {
> if (!cnt) return a; // or maybe return 0
> a[cnt-1] = 0;
> return strncpy(a,b,cnt-1);
> }
>
>> classes instead.
>>
>> Good article about XXXncpy functions:
>>
>> "How can code that tries to prevent a buffer overflow end up causing
>> one?"
>>
http://blogs.msdn.com/oldnewthing/archive/2005/01/07/348437.aspx
>>
>
>