开发者

sprintf_s crashes

开发者 https://www.devze.com 2023-03-03 15:10 出处:网络
I am getting a crash while executing the following code ocassionally at sprintf_s. This code was working many years without any problems. When I gave the sizein strcat_s and sprintf_s as in the开发者_

I am getting a crash while executing the following code ocassionally at sprintf_s. This code was working many years without any problems. When I gave the size in strcat_s and sprintf_s as in the开发者_如何学JAVA statements below, the crash is not appearing. What could be the reason for this?

strcat_s(sztmpCurrDate,100,sztmpCurrTime); sprintf_s(sztmpCurrDate,100,"%s:%0.3d",sztmpCurrDate,curTime.wMilliseconds););

char sztmpCurrDate[100] = "";
char sztmpCurrTime[100] = "";
SYSTEMTIME curTime;
GetLocalTime(&curTime); 
GetLocalTime(&curTime); 
GetDateFormat(LOCALE_USER_DEFAULT,
                                DATE_SHORTDATE,
                                &curTime,
                                NULL,
                                sztmpCurrDate,
                                100);

GetTimeFormat(LOCALE_USER_DEFAULT,
                          TIME_FORCE24HOURFORMAT,
                          &curTime,
                          "HH':'mm':'ss",
                          sztmpCurrTime,
                          100);

strcat_s(sztmpCurrDate," ");
strcat_s(sztmpCurrDate,sztmpCurrTime);
sprintf_s(sztmpCurrDate,"%s:%0.3d",sztmpCurrDate,curTime.wMilliseconds);


From the documentation for sprintf_s:

If copying occurs between strings that overlap, the behavior is undefined.

Your code:

sprintf_s(sztmpCurrDate,"%s:%0.3d",sztmpCurrDate,curTime.wMilliseconds);

copies from the source to the destination sztmpCurrDate. Also, you haven't specified the size of the destination string, which is required by sprintf_s (I don't know how your code even compiled like that). Try:

sprintf_s(sztmpCurrDate + strlen(sztmpCurrDate), 100-strlen(sztmpCurrDate),
          ":%0.3d",curTime.wMilliseconds);

A better approach, since you're using C++, is to use std::string and then you won't have to worry about this sort of C string manipulation error.


Wrong syntax!!!

Second argument of sprintf_s is length of your buffer and in your case when program crashes you provided pointer to C string (char *). This is absolutely syntax error.

The compiler probably issued a warning, but sadly has let this pass. This is because sprintf_s takes three arguments + variable number of arguments. In your wrong case you provided three arguments so the compiler was satisfied, but he treated your "format string" as "number of arguments" and sztmpCurrDate as "format string".

If you used any other function with fixed number of arguments and you provided less than needed this would be a compile error.


You are probably using the incorrect version:

sprintf_s(sztmpCurrDate,"%s:%0.3d",sztmpCurrDate,curTime.wMilliseconds);

Instead it should be:

int slen = strlen(sztmpCurrDate) + 1;

sprintf_s(sztmpCurrDate, slen, "%s:%0.3d",sztmpCurrDate,curTime.wMilliseconds);

Refer to this sprintf_s for more information.


The code worked correctly, the code is not correct.

sprintf(sztmpCurrDate,"%s:%0.3d",sztmpCurrDate,curTime.wMilliseconds);

Rewrite as

sprintf(&sztmpCurrDate[strlen(sztmpCurrDate)],"%0.3d",curTime.wMilliseconds);

And for home work tell us why .....

0

精彩评论

暂无评论...
验证码 换一张
取 消