Click here to Skip to main content
15,903,030 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C++
void myInvalidParameterHandler(const wchar_t* expression,
   const wchar_t* function, 
   const wchar_t* file, 
   unsigned int line, 
   uintptr_t pReserved)
{
	wprintf(L"Invalid parameter detected in function %s."
            L" File: %s Line: %d\n", function, file, line);
   wprintf(L"Expression: %s\n", expression);

   
}


void main()
{
	struct tm when;
	int yy=00, mm=00,dd=00;
	char date[20];
	errno_t errNo;
	strcpy(date,"2010-05-03");
	sscanf_s(date,"%4d-%2d-%2d",&yy,&mm,&dd);
	printf("year : %4d\n",yy);
	printf("month : %2d\n",mm);
	printf("date : %2d\n",dd);

	when.tm_year = yy - 1900;
	when.tm_mon = mm - 1;
	when.tm_mday = dd;
	when.tm_hour = 0;
	when.tm_min = 0;
	when.tm_sec = 0;
	when.tm_wday = 0;
	
	_invalid_parameter_handler oldHandler, newHandler;
	newHandler = myInvalidParameterHandler;
	oldHandler = _set_invalid_parameter_handler(newHandler);
	_CrtSetReportMode(_CRT_ASSERT , 0);
	errNo = asctime_s(date,32,&when);
	printf("date is right...\n");

}


It gives the proper output but when the application terminates , error is "Run-Time Check Failure #2 - Stack around the variable 'dd' was corrupted. ".... what is solution??
Posted
Updated 2-May-11 3:15am
v3
Comments
[no name] 3-May-11 0:29am    
Rick has solution

1 solution

When you make the call to asctime_s, you tell the function that your buffer is 32 bytes. However, it is only 20 bytes. This is causing the function to overrun your buffer.
 
Share this answer
 
Comments
Ruchira Patel 3-May-11 2:30am    
Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900