2013-07-24 5 views
-2

Win32 API를 사용하여 VC++에서 프로그램 개발, Windows 7 32 비트 OS 용. 보안 복사를 위해 string.h에 언급 된 strncpy_s 함수를 사용했지만 일부 컴퓨터에서는 충돌이 발생합니다. 여기strncpy_s 함수의 비정상적인 동작

#if __STDC_WANT_SECURE_LIB__ 
_Check_return_wat_ _CRTIMP_ALTERNATIVE errno_t __cdecl strncpy_s(_Out_z_cap_(_SizeInBytes) char * _Dst, _In_ rsize_t _SizeInBytes, _In_z_ const char * _Src, _In_ rsize_t _MaxCount); 
#endif 

는이를 바탕으로 스택 트레이스

> msvcr100.dll!malloc(unsigned int size) Line 89 + 0x3b bytes C 
    mfc100u.dll!operator new(unsigned int nSize) Line 323 + 0x5 bytes C++ 
    MyTest.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Copy(unsigned int _Newsize, unsigned int _Oldlen) Line 1933 + 0x16 bytes C++ 
    MyTest.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign(const char * _Ptr, unsigned int _Count) Line 920 + 0x26 bytes C++ 
    MyTest.exe!std::basic_stringbuf<char,std::char_traits<char>,std::allocator<char> >::str() Line 97 + 0x2a bytes C++ 
    MyTest.exe!std::basic_ostringstream<char,std::char_traits<char>,std::allocator<char> >::str() Line 593 + 0x12 bytes C++ 
    MyTest.exe!Trace<TraceToFile>::~Trace<TraceToFile>() Line 174 + 0xa bytes C++ 
    MyTest.exe!CCommonUtilities::sendDataToClient(std::basic_string<char,std::char_traits<char>,std::allocator<char> > strJson, int nCommandID, std::basic_string<char,std::char_traits<char>,std::allocator<char> > strFuncName, int nLineNumber, int nClientKey) Line 320 C++ 
    MyTest.exe!CConnectionManager::AddConnectionThread(void * p_Param) Line 270 + 0x74 bytes C++ 

이다, 할 수있는이 strncpy_s 또는 프로그래밍 오류에 문제가있는 경우 누군가 포인트는?

코드를 디버그하려고해도 문제가 발생하지 않습니다. 우리가이 바이너리를 실행할 때 항상 올 것입니다.

많은 분들께 미리 감사드립니다.

+3

제공되는 정보로는 문제를 진단하기에 충분하지 않지만'strncpy_s '에 버그가있는 경우 매우 놀랍습니다. 코드에 버그가 있다는 가정하에 진행하는 것이 안전해야합니다. 디버그 빌드를 실행하면 버그를 숨기고 릴리스 빌드에 로깅 문을 추가하십시오. – simonc

+0

왜 C++ 코드에서'strncpy_s'를 사용하고 있습니까? – RedX

+0

어둠 속에서 짐작할 수 있겠지만, 여러분의 코드에서 뭔가가 초기화되지 않았습니다 (종종 디버그/릴리즈의 차이점을 설명합니다). malloc은 엄청난 숫자를 얻고 폭발하게됩니다. 코드를 보지 않고 누가 말할 수 있습니까? –

답변

2

크래시가 strncpy_s에 없으므로 스택 추적에 표시되지 않습니다.

크래시가 손상된 메모리에 있습니다. Trace<> 소멸자에서개체를 만들어 메모리를 할당하려고 시도하고 힙이 손상되었음을 발견했습니다.

손상을 찾기위한 시작 지점으로 코드 ~Trace<> 또는 CCommonUtilities::sendDataToClient()을 확인하십시오.

strncpy_n()이라고 생각하십니까?

0

strncpy_s의 "안전성"은 유효 입력을 제공하는 호출자에서와 같이 기능을 구현할 때 그다지 그다지 중요하지 않으며 기능이 제한을 초과하지 않는지 검사합니다.

즉, strncpy_s에 대상 (두 번째 매개 변수)의 크기를 지정하거나 잘못된 주소를 원본 또는 대상으로 전달하면 어떤 식 으로든 잘못됩니다.