How can I prevent strncpy_s from populating the destination buffer in the debug build?

I support a rather large snippet of old code that still uses strncpy. Now I have begun the process of replacing use strncpywith a safe copy of it strncpy_s. I noticed that it strncpy_sfills the destination buffer using values ​​of -2, but only in debug builds! There are no gaps in the release build.

For instance:

char buffer[3];
// buffer becomes 00000000  00000000  00000000
memset(buffer, 0, sizeof(buffer));
// buffer becomes 01100001  00000000  11111110
//                97 ('a')  0        -2
strncpy_s(buffer, sizeof(buffer), "a", _TRUNCATE);
// i is -2
int i = buffer[2];

MSDN docs don't mention this add-on, and in my case this is really what I don't want, because my old code relies on the fact that the zeroed portions of the buffer remain nullified and are not overwritten while copying lines.

strncpy_s ?

, Visual Studio 2010, Visual Studio 2013.

+4
2
+4

strncpy_s , . strncpy , 0. , strncpy_s.

, strncpy_s strncpy , .

strncpy strncpy_s. strcpy, snprintf memcpy. , , .

" " , . "" , ; , .

, strncpy, , , .

+7

All Articles