Re: Crash when reading a CString out of a CArchive

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 07/23/04


Date: Fri, 23 Jul 2004 17:06:28 -0400

Looks to me like MFC is doing exactly what it should do. You create an archived CString
whose length is EEEEFFFF by faking the data, then you read it back. So what is the
surprise here?

If you want to read something back as a CString, you must have it saved in the appropriate
format for a CString.

I don't even understand the comments. Why do you think 0xFF forces MFC to read WORD string
length, and then you force MFC to read a DWORD string length, and then you give a negative
number that pretends to be a DWORD, which is fine, but of course it is completely
nonsensical as a string length.

Frankly, the whole piece of code is suspect. Why are you wasting effort faking serializing
strings to memory just so you can read them back? If you want to write a string, why now
write
        ar << s;
to write the string you expect to read back; then writing something like
        ar >> s

would make sense, because you have a genuine serialized string, not some kludged-up
illegal-format set of nonsense bits.

Actually, I have successfully avoided all forms of using serialization for as long as I've
used MFC. There has to be significant compelling reason to do so, and I have yet to
discover it. These days, I use XML to exchange data.
                                        joe

On Fri, 23 Jul 2004 15:21:34 +0200, Tobias Güntner <guentner@schleissheimer.de> wrote:

>Hi!
>I believe there is a bug in MFC 4.2 (I use VC6 SP6 on Intel x86).
>Have a look at my example code:
>
>void Test()
>{
> // Create a virtual test file (The problem
> // also occurs when reading a CString out
> // of a real file. I use a CMemFile here
> // to create a self-contained test-case)
>
> BYTE data[] = {
> 0xFF, // Force MFC to read WORD string length
> 0xFF, 0xFF, // Forge MFC to read DWORD string length
> 0xFF, 0xFF, 0xEE, 0xEE, // A negative number that pretends to be a DWORD
> };
>
> CMemFile file(data, sizeof(data));
> CArchive ar(&file, CArchive::load);
> CString s;
>
> // Try to read a string out of the "file".
> // This causes an access violation.
> ar >> s;
>}
>
>
>The last line crashes the program, i.e. an access violation occurs.
>The crash is caused by the following line in CString::GetBufferSetLength:
> m_pchData[nNewLength] = '\0';
>nNewLength is -286326785 (or 0xeeeeffff if you prefer). The previous
>call to GetBuffer did not allocate enough memory (it simply asserted
>that the new buffer length is >=0 and then returned silently - this
>check is obviously useless in a release build).
>IMHO it should a) accept an unsigned int as parameter or b) throw a
>CMemoryException because it cannot possibly allocate -286326785 Bytes.
>
>Is there a fix for that behavior?
>
>Regards,
>Tobias

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



Relevant Pages

  • Re: Creating and setting registry key value
    ... I have now discovered the mfc is not nearly so user friendly as the JDK. ... > There are significant poor style usages in the code below. ... But if you need a variable, CString is a better ... string to a CString? ...
    (microsoft.public.vc.mfc)
  • Re: Crash when reading a CString out of a CArchive
    ... the belief that an attempt to read a CString will actually get valid data. ... >> Looks to me like MFC is doing exactly what it should do. ... >there is no string, that reading a string of astronomical size is ... >string serialization routines start around line 150 or so. ...
    (microsoft.public.vc.mfc)
  • Re: error in read editcontrol and EndDialog
    ... If you are developing an MFC project created by AppWizard, you should be able to just use CString without #including any header file manually. ... If you are developing a Win32 C++ project, you could use CString, too. ... LPSTR fileName just defines a pointer to a raw C string. ...
    (microsoft.public.vc.mfc)
  • Re: How to: optional CString parameter
    ... Then, the question I would ask you is, why force the user of your API to use CString or MFC at all? ... why assume that the user of your API would be using CString. ... a larger API that you might allow other developers to use for various types of projects, then the const CString& approach works great. ...
    (microsoft.public.vc.mfc)
  • Re: Safe string functions in VC6
    ... The DDK's header links to ntstrsafe.lib ... CString is out of the question in this application - even if it was new code. ... especially since the status of MFC is shifting. ... our code will be ready if the string functions are ...
    (microsoft.public.vc.mfc)