Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is this called Buffre overrun

Is this called Buffre overrun

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelpworkspace
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vipin_nvk
    wrote on last edited by
    #1

    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.

    C O CPalliniC M 4 Replies Last reply
    0
    • V vipin_nvk

      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.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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]

      1 Reply Last reply
      0
      • V vipin_nvk

        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.

        O Offline
        O Offline
        Optimus Chaos
        wrote on last edited by
        #3

        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") );
        }

        1 Reply Last reply
        0
        • V vipin_nvk

          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.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          The GetBuffer method of CString 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 the GetBuffer 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.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • V vipin_nvk

            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.

            M Offline
            M Offline
            Matthew Faithfull
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups