There\'re certain cases when I really need strncpy()
funcitonalty - for example I have a function in a predefined interface that is passed an address of the buf
You might use memcpy_s instead.
HRESULT someFunction( char* buffer, size_t length )
{
const char* toCopy = ...
size_t actualLength = strlen( toCopy );
if( actualLength > length ) {
return E_UNEXPECTED; // doesn't fit, can't do anything reasonable
}
else if ( actualLength < length ) {
actualLength++; // copy null terminator too
}
memcpy_s( buffer, length, toCopy, actualLength );
return S_OK;
}
The problem you're facing here is that your function is unsafe in itself, just like strncpy()
is. It is unsafe because callers of your function might forget that the returned strings are not null terminated. If this really is the desired behavior of your function I recommend not to define _CRT_SECURE_NO_WARNINGS
and disable the warnings globally but use #pragmas
instead:
// document here exactly why you can not use strncpy_s
#pragma warning( push )
#pragma warning( disable : 4996 )
// your code that uses strncpy instead of strncpy_s
#pragma warning( pop )
That way you disable those warnings only for those situations where you absolutely have to use unsafe functions.
You want null-termination where actualLength < length
, and no null-termination where actualLength == length
, right?
So use strncpy_s
for the case where actualLength < length
and memcpy_s
where actualLength == length
.
Trying to use str*cpy*()
functions for scenarios with a fixed destination buffer is a common misconception. The deal here is that those functions "copy until a null character or other condition occurs". In this scenario there's this code in place:
size_t actualLength = strlen( toCopy );
if( actualLength > length ) {
return E_UNEXPECTED; // doesn't fit, can't do anything reasonable
}
and so the code knows the actual string length before it proceeds to copying. And once length is known it only makes sense to use memcpy()
which is straightforward and concise for such scenarios and as a side effect also faster since it is allowed to copy more than one character at a time and doesn't check each character for null terminator.
HRESULT someFunction( char* buffer, size_t length )
{
const char* toCopy = ...
size_t actualLength = strlen( toCopy );
if( actualLength > length ) {
return E_UNEXPECTED; // doesn't fit, can't do anything reasonable
}
memcpy( buffer, toCopy, min( length, actualLength + 1 ) );
return S_OK;
}
So the solution is to just forget both strncpy()
and strncpy_s()
and use memcpy()
instead.