Re: Crash when reading a CString out of a CArchive
From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 07/23/04
- Next message: Joseph M. Newcomer: "Re: Access Denied problem with CreateFile"
- Previous message: Joseph M. Newcomer: "Re: Another way for waiting for a AfxBeginThread thread to end"
- In reply to: Tobias Güntner: "Crash when reading a CString out of a CArchive"
- Next in thread: Tobias Güntner: "Re: Crash when reading a CString out of a CArchive"
- Reply: Tobias Güntner: "Re: Crash when reading a CString out of a CArchive"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Joseph M. Newcomer: "Re: Access Denied problem with CreateFile"
- Previous message: Joseph M. Newcomer: "Re: Another way for waiting for a AfxBeginThread thread to end"
- In reply to: Tobias Güntner: "Crash when reading a CString out of a CArchive"
- Next in thread: Tobias Güntner: "Re: Crash when reading a CString out of a CArchive"
- Reply: Tobias Güntner: "Re: Crash when reading a CString out of a CArchive"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|