Is this called Buffre overrun
-
Hello I have a strange problem in my code. I call this function to return me the current date and time. But, what I have observed is that once this piece of code gets exceuted in my workspace, all the CString variables in the workspace get initialized with the current date time by default (though they have been intialized with empty strings) Is this because of the concept called buffer overrun. Is there anything like buffer overrun happening here where in the memory is getting corrupted because we are not specifying the size of the source / target buffers into which the strings have to be copied. Please do clarify. Find the code snippet attached.
CString CLogger::GetCurrentDateTime() { CString strCurrDateTime,strTemp; SYSTEMTIME stCurrent,stLocal; GetSystemTime(&stCurrent); SystemTimeToTzSpecificLocalTime(NULL, &stCurrent, &stLocal); wsprintf(strCurrDateTime.GetBuffer(0),_T("%02d/%02d/%d %02d:%02d"),stLocal.wDay, stLocal.wMonth, stLocal.wYear,stLocal.wHour, stLocal.wMinute); return strCurrDateTime; }
Regards, Vipin. -
Hello I have a strange problem in my code. I call this function to return me the current date and time. But, what I have observed is that once this piece of code gets exceuted in my workspace, all the CString variables in the workspace get initialized with the current date time by default (though they have been intialized with empty strings) Is this because of the concept called buffer overrun. Is there anything like buffer overrun happening here where in the memory is getting corrupted because we are not specifying the size of the source / target buffers into which the strings have to be copied. Please do clarify. Find the code snippet attached.
CString CLogger::GetCurrentDateTime() { CString strCurrDateTime,strTemp; SYSTEMTIME stCurrent,stLocal; GetSystemTime(&stCurrent); SystemTimeToTzSpecificLocalTime(NULL, &stCurrent, &stLocal); wsprintf(strCurrDateTime.GetBuffer(0),_T("%02d/%02d/%d %02d:%02d"),stLocal.wDay, stLocal.wMonth, stLocal.wYear,stLocal.wHour, stLocal.wMinute); return strCurrDateTime; }
Regards, Vipin.Never use GetBuffer for doing things like that (and furthermore, you request a size of 0, so you will probably screw everything by write something much larger in the buffer). See the doc[^] You should use the Format[^] function instead.
Cédric Moonen Software developer
Charting control [v1.2] -
Hello I have a strange problem in my code. I call this function to return me the current date and time. But, what I have observed is that once this piece of code gets exceuted in my workspace, all the CString variables in the workspace get initialized with the current date time by default (though they have been intialized with empty strings) Is this because of the concept called buffer overrun. Is there anything like buffer overrun happening here where in the memory is getting corrupted because we are not specifying the size of the source / target buffers into which the strings have to be copied. Please do clarify. Find the code snippet attached.
CString CLogger::GetCurrentDateTime() { CString strCurrDateTime,strTemp; SYSTEMTIME stCurrent,stLocal; GetSystemTime(&stCurrent); SystemTimeToTzSpecificLocalTime(NULL, &stCurrent, &stLocal); wsprintf(strCurrDateTime.GetBuffer(0),_T("%02d/%02d/%d %02d:%02d"),stLocal.wDay, stLocal.wMonth, stLocal.wYear,stLocal.wHour, stLocal.wMinute); return strCurrDateTime; }
Regards, Vipin.Hi, I think you should consider taking COleDatetime for this. Because you could either use GetCurrentTime or construct a new COleDateTime from your SYSTEMTIME structure. Afterwards you can use the Format method to assign it to your CString. example:
CString CLogger::GetCurrentDateTime()
{
COleDateTime dtCurrent = COleDateTime::GetCurrentTime();return dtCurrent.Format ( _T("%d/%m/%Y %H:%M") );
}or
CString CLogger::GetCurrentDateTime()
{
SYSTEMTIME stCurrent,stLocal;
GetSystemTime(&stCurrent);
SystemTimeToTzSpecificLocalTime(NULL, &stCurrent, &stLocal);COleDateTime time ( stLocal );
return time.Format ( _T("%d/%m/%Y %H:%M") );
} -
Hello I have a strange problem in my code. I call this function to return me the current date and time. But, what I have observed is that once this piece of code gets exceuted in my workspace, all the CString variables in the workspace get initialized with the current date time by default (though they have been intialized with empty strings) Is this because of the concept called buffer overrun. Is there anything like buffer overrun happening here where in the memory is getting corrupted because we are not specifying the size of the source / target buffers into which the strings have to be copied. Please do clarify. Find the code snippet attached.
CString CLogger::GetCurrentDateTime() { CString strCurrDateTime,strTemp; SYSTEMTIME stCurrent,stLocal; GetSystemTime(&stCurrent); SystemTimeToTzSpecificLocalTime(NULL, &stCurrent, &stLocal); wsprintf(strCurrDateTime.GetBuffer(0),_T("%02d/%02d/%d %02d:%02d"),stLocal.wDay, stLocal.wMonth, stLocal.wYear,stLocal.wHour, stLocal.wMinute); return strCurrDateTime; }
Regards, Vipin.The
GetBuffer
method ofCString
is misused in your code. you're asking a zero-length buffer to CString and filling it with (a non-zero number of) characters. You've to ask for a larger buffer (or, and will be better, don't use at all theGetBuffer
method). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hello I have a strange problem in my code. I call this function to return me the current date and time. But, what I have observed is that once this piece of code gets exceuted in my workspace, all the CString variables in the workspace get initialized with the current date time by default (though they have been intialized with empty strings) Is this because of the concept called buffer overrun. Is there anything like buffer overrun happening here where in the memory is getting corrupted because we are not specifying the size of the source / target buffers into which the strings have to be copied. Please do clarify. Find the code snippet attached.
CString CLogger::GetCurrentDateTime() { CString strCurrDateTime,strTemp; SYSTEMTIME stCurrent,stLocal; GetSystemTime(&stCurrent); SystemTimeToTzSpecificLocalTime(NULL, &stCurrent, &stLocal); wsprintf(strCurrDateTime.GetBuffer(0),_T("%02d/%02d/%d %02d:%02d"),stLocal.wDay, stLocal.wMonth, stLocal.wYear,stLocal.wHour, stLocal.wMinute); return strCurrDateTime; }
Regards, Vipin.Just in case you were still wandering;), the reason you see the Date Time text in every uninitialised CString afterwards is that CString has a peculiar internal static member which represents the empty string. If you've overwritten this as it appears by overrunning the 0 length buffer returned from GetBuffer(0) then all CStrings which get auto initailised to this empty string will appear to contain the Date Time string.
Nothing is exactly what it seems but everything with seems can be unpicked.