strncpy, _strncpy_l, wcsncpy, _wcsncpy_l, _mbsncpy, _mbsncpy_l

한 문자열의 문자를 복사 합니다.보다 안전한 버전의이 함수를 사용할 수 있습니다. see strncpy_s, _strncpy_s_l, wcsncpy_s, _wcsncpy_s_l, _mbsncpy_s, _mbsncpy_s_l.

중요중요

_mbsncpy및 _mbsncpy_l 를 실행 하는 응용 프로그램에서 사용할 수 없습니다는 Windows 런타임.자세한 내용은 /zw에 지원 되는 CRT 함수.

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

매개 변수

  • strDest
    대상 문자열입니다.

  • strSource
    원본 문자열입니다.

  • count
    복사할 문자 수입니다.

  • locale
    사용 하는 로캘.

반환 값

strDest를 반환합니다.반환 값이 없는 오류를 나타내기 위해 예약 되어 있습니다.

설명

strncpy 함수 복사 초기 count 자 strSource 에 strDest 및 반환 strDest.경우 count 의 길이 보다 작거나 strSource, null 문자가 복사 된 문자열을 자동으로 추가 되지 않습니다.경우 count 의 길이 보다 큰 경우 strSource, 대상 문자열에 null 문자 길이까지 채운 count.동작을 strncpy 원본 및 대상 문자열 겹칠 경우 정의 되어 있지 않습니다.

보안 정보보안 정보

strncpy충분 한 공간을 확인 하지 않습니다 strDest. 이렇게 하면 잠재적인 버퍼 오버런 원인이 있습니다.count 인수 제한 문자 복사. 크기에 제한이 없는 strDest.다음 예제를 참조하십시오.자세한 내용은 버퍼 오버런 방지.

경우 strDest 또는 strSource 되는 NULL 포인터를 경우 count 보다 작거나 0에 잘못 된 매개 변수 처리기의 설명에 따라 호출 됩니다 매개 변수 유효성 검사.이러한 함수 실행을 계속 허용 되 면-1을 반환 하 고 설정 errno 에EINVAL

wcsncpy및 _mbsncpy 와이드 및 멀티 바이트 문자 버전의 strncpy.인수 및 반환 값을 wcsncpy 및 _mbsncpy 에 따라 달라 집니다.이러한 6 개의 함수 그렇지에 동일 하 게 동작 합니다.

이러한 함수 버전의 _l 접미사는 해당 로캘 종속 동작에 대 한 현재 로캘 대신 전달 된 로캘을 사용 하는 것을 제외 하 고 동일 합니다.자세한 내용은 로캘을 참조하십시오.

C + +에서는 이러한 함수 최신, 보안 대응 함수를 호출 하는 템플릿 오버 로드 되어 있습니다.자세한 내용은 보안 템플릿 오버 로드을 참조하십시오.

일반 텍스트 루틴 매핑

TCHAR입니다.H 루틴

_UNICODE 및 _Mbcs가 정의 되어 있지 않습니다

_Mbcs가 정의

_Unicode가 정의

_tcsncpy

strncpy

_mbsnbcpy

wcsncpy

_tcsncpy_l

_strncpy_l

_mbsnbcpy_l

_wcsncpy_l

[!참고]

_strncpy_l및 _wcsncpy_l 가 로캘 종속 하지 않습니다. 에 제공 된 _tcsncpy_l 하며 직접 호출할 수 없습니다.

요구 사항

루틴

필수 헤더

strncpy

<string.h>

wcsncpy

<string.h> 또는 <wchar.h>

_mbsncpy, _mbsncpy_l

<mbstring.h>

추가적인 플랫폼 호환성에 대 한 내용은 호환성.

예제

다음 예제에서는 strncpy 및 잘못 어떻게 프로그램 버그 및 보안 문제를 일으키는 것으로 사용 될 수 있습니다.각 호출에 대해 경고를 생성 하는 컴파일러 strncpy 와 유사한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

  
  

자동 변수 레이아웃 오류 감지 및 코드 보호 수준에서 변경 된 컴파일러 설정을 사용 하 여 달라질 수 있습니다.이 예제에서는 다른 컴파일 환경에서 또는 다른 컴파일러 옵션을 사용 하면 서로 다른 결과가 있을 수 있습니다.

해당 .NET Framework 항목

System::String::Copy

참고 항목

참조

문자열 조작 (CRT)

로캘

멀티 바이트 문자 시퀀스로 해석

_mbsnbcpy, _mbsnbcpy_l

strcat, wcscat, _mbscat

strcmp, wcscmp, _mbscmp

strcpy, wcscpy, _mbscpy

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

_mbscpy_s, wcscpy_s strcpy_s