strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l
Copy characters of one string to another. More secure versions of these functions are available; see strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l.
Important
_mbsncpy and _mbsncpy_l cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported with /ZW.
char *strncpy(
char *strDest,
const char *strSource,
size_t count
);
char *_strncpy_l(
char *strDest,
const char *strSource,
size_t count,
locale_t locale
);
wchar_t *wcsncpy(
wchar_t *strDest,
const wchar_t *strSource,
size_t count
);
wchar_t *_wcsncpy_l(
wchar_t *strDest,
const wchar_t *strSource,
size_t count,
locale_t locale
);
unsigned char *_mbsncpy(
unsigned char *strDest,
const unsigned char *strSource,
size_t count
);
unsigned char *_mbsncpy_l(
unsigned char *strDest,
const unsigned char *strSource,
size_t count,
_locale_t locale
);
template <size_t size>
char *strncpy(
char (&strDest)[size],
const char *strSource,
size_t count
); // C++ only
template <size_t size>
char *_strncpy_l(
char (&strDest)[size],
const char *strSource,
size_t count,
locale_t locale
); // C++ only
template <size_t size>
wchar_t *wcsncpy(
wchar_t (&strDest)[size],
const wchar_t *strSource,
size_t count
); // C++ only
template <size_t size>
wchar_t *_wcsncpy_l(
wchar_t (&strDest)[size],
const wchar_t *strSource,
size_t count,
locale_t locale
); // C++ only
template <size_t size>
unsigned char *_mbsncpy(
unsigned char (&strDest)[size],
const unsigned char *strSource,
size_t count
); // C++ only
template <size_t size>
unsigned char *_mbsncpy_l(
unsigned char (&strDest)[size],
const unsigned char *strSource,
size_t count,
_locale_t locale
); // C++ only
Parameters
strDest
Destination string.strSource
Source string.count
Number of characters to be copied.locale
Locale to use.
Return Value
Returns strDest. No return value is reserved to indicate an error.
Remarks
The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.
Security Note |
---|
strncpy does not check for sufficient space in strDest; this makes it a potential cause of buffer overruns. The count argument limits the number of characters copied; it is not a limit on the size of strDest. See the following example. For more information, see Avoiding Buffer Overruns. |
If strDest or strSource is a NULL pointer, or if count is less than or equal to zero, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL
wcsncpy and _mbsncpy are wide-character and multibyte-character versions of strncpy. The arguments and return value of wcsncpy and _mbsncpy vary accordingly. These six functions behave identically otherwise.
The versions of these functions with the _l suffix are identical except that they use the locale passed in instead of the current locale for their locale-dependent behavior. For more information, see Locale.
In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. For more information, see Secure Template Overloads.
Generic-Text Routine Mappings
TCHAR.H routine |
_UNICODE & _MBCS not defined |
_MBCS defined |
_UNICODE defined |
---|---|---|---|
_tcsncpy |
strncpy |
_mbsnbcpy |
wcsncpy |
_tcsncpy_l |
_strncpy_l |
_mbsnbcpy_l |
_wcsncpy_l |
Note
_strncpy_l and _wcsncpy_l have no locale dependence; they are provided just for _tcsncpy_l and are not intended to be called directly.
Requirements
Routine |
Required header |
---|---|
strncpy |
<string.h> |
wcsncpy |
<string.h> or <wchar.h> |
_mbsncpy, _mbsncpy_l |
<mbstring.h> |
For additional platform compatibility information, see Compatibility.
Example
The following example demonstrates the use of strncpy and how it can be misused to cause program bugs and security issues. The compiler generates a warning for each call to strncpy similar to crt_strncpy_x86.c(15) : warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
// crt_strncpy_x86.c
// Use this command in an x86 developer command prompt to compile:
// cl /TC /W3 crt_strncpy_x86.c
#include <stdio.h>
#include <string.h>
int main() {
char t[20];
char s[20];
char *p = 0, *q = 0;
strcpy_s(s, sizeof(s), "AA BB CC");
// Note: strncpy is deprecated; consider using strncpy_s instead
strncpy(s, "aa", 2); // "aa BB CC" C4996
strncpy(s + 3, "bb", 2); // "aa bb CC" C4996
strncpy(s, "ZZ", 3); // "ZZ", C4996
// count greater than strSource, null added
printf("%s\n", s);
strcpy_s(s, sizeof(s), "AA BB CC");
p = strstr(s, "BB");
q = strstr(s, "CC");
strncpy(s, "aa", p - s - 1); // "aa BB CC" C4996
strncpy(p, "bb", q - p - 1); // "aa bb CC" C4996
strncpy(q, "cc", q - s); // "aa bb cc" C4996
strncpy(q, "dd", strlen(q)); // "aa bb dd" C4996
printf("%s\n", s);
// some problems with strncpy
strcpy_s(s, sizeof(s), "test");
strncpy(t, "this is a very long string", 20 ); // C4996
// Danger: at this point, t has no terminating null,
// so the printf continues until it runs into one.
// In this case, it will print "this is a very long test"
printf("%s\n", t);
strcpy_s(t, sizeof(t), "dogs like cats");
printf("%s\n", t);
strncpy(t + 10, "to chase cars.", 14); // C4996
printf("%s\n", t);
// strncpy has caused a buffer overrun and corrupted string s
printf("Buffer overrun: s = '%s' (should be 'test')\n", s);
// Since the stack grows from higher to lower addresses, buffer
// overruns can corrupt function return addresses on the stack,
// which can be exploited to run arbitrary code.
}
Output
ZZ aa bb dd this is a very long test dogs like cats dogs like to chase cars. Buffer overrun: s = 'ars.' (should be 'test')
The layout of automatic variables and the level of error detection and code protection can vary with changed compiler settings. This example may have different results when built in other compilation environments or with other compiler options.
.NET Framework Equivalent
See Also
Reference
Interpretation of Multibyte-Character Sequences
strncat, _strncat_l, wcsncat, wcsncat_l, _mbsncat _mbsncat_l
strncmp, wcsncmp, _mbsncmp, _mbsncmp_l
_strnicmp, _wcsnicmp, _mbsnicmp, _strnicmp_l, _wcsnicmp_l, _mbsnicmp_l
strrchr, wcsrchr, _mbsrchr, _mbsrchr_l
_strset, _strset_l, _wcsset, _wcsset_l, _mbsset, _mbsset_l
strspn, wcsspn, _mbsspn, _mbsspn_l
strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l