15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


CArchive::ReadString(CString& rString) ?Bug?

Andrew Alexander -- 100446.1321@compuserve.com
Tuesday, January 23, 1996

I am using VC++ 4 MFC 4

For your information:

CArchive::ReadString(CString& rString)  appears to be flawed.
I am using it to read in lines of text of various lengths
sometimes larger than 128 characters. The first line over
this length is fine. But the next line over this size is
truncated.
This occurs due to the flawed use of the Allocated length
to calculate where to store the next 128 byte block.
The Allocated length may be longer because of previous use
of the string, but the function expects it to step up in size
only due to reading in the current string.

Please see attached source and Memory dump.

Solutions: Well I deallocated the string
before the next call. I guess I could change the MFC
but....

Just In case I am doing something stupid I attach my code below.

---------------------------------------------------------------------------------
-------
BOOL CArchive::ReadString(CString& rString)
{
	rString = &afxChNil;    // empty string without deallocating
	const int nMaxSize = 128;
	LPTSTR lpsz = rString.GetBuffer(nMaxSize);
	LPTSTR lpszResult;
	for (;;)
	{
		lpszResult = ReadString(lpsz, nMaxSize);

		// if string is read completely or EOF
		if (lpszResult == NULL || lstrlen(lpsz) < nMaxSize-1)
			break;

// Problem here
		int nAllocLen = rString.GetAllocLength();
		rString.ReleaseBuffer();
		lpsz = rString.GetBuffer(nMaxSize + nAllocLen) + nAllocLen;
// End Problem
	}
	rString.ReleaseBuffer();

	return lpszResult != NULL;
}

Partial Memory Dump.
0065A9D3  27 4E 6F 72 74 68 61  'Northa
0065A9DA  6D 70 74 6F 6E 73 68  mptonsh
0065A9E1  69 72 65 27 2C 27 55  ire','U
0065A9E8  4B 27 2C 27 4E 4E 38  K','NN8
0065A9EF  20 33 46 47 27 2C 31   3FG',1
0065A9F6  2E 30 00 CD CD CD CD  .0.IIII
0065A9FD  CD CD CD CD CD CD CD  IIIIIII
0065AA04  CD CD CD CD CD CD CD  IIIIIII
0065AA0B  CD CD CD CD CD CD CD  IIIIIII
0065AA12  CD CD CD CD CD CD CD  IIIIIII
0065AA19  CD CD CD CD CD CD CD  IIIIIII
0065AA20  CD CD CD CD CD CD CD  IIIIIII
0065AA27  CD CD CD CD CD CD CD  IIIIIII
0065AA2E  CD CD CD CD CD CD CD  IIIIIII
0065AA35  CD CD CD CD CD CD CD  IIIIIII
0065AA3C  CD CD CD CD CD CD CD  IIIIIII
0065AA43  CD CD CD CD CD CD CD  IIIIIII
0065AA4A  CD CD CD CD CD CD CD  IIIIIII
0065AA51  CD CD CD CD CD CD CD  IIIIIII
0065AA58  CD CD CD CD CD CD CD  IIIIIII
0065AA5F  CD CD CD CD CD CD CD  IIIIIII
0065AA66  CD CD CD CD CD CD CD  IIIIIII
0065AA6D  CD CD CD CD CD CD CD  IIIIIII
0065AA74  CD CD CD CD 29 00 CD  IIII).I
0065AA7B  CD CD CD CD CD CD CD  IIIIIII

You will notice the bracket that
is meant to be the last character on the input line
is 128 bytes offset from where it should be.

=========================================
This is my cut down code. Its just a dialog box app
with following code entered. Try it if you like.

BOOL CGoSQLApp::InitInstance()
{

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a
shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC
statically
#endif

	static char BASED_CODE szFilter[] = "Import Files (*.imp) | *.imp | All
Files (*.*) | *.* ||";
	CFileDialog Import(TRUE, "imp", "*.*", OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, szFilter);

	CFile f;
	CString ImportFile;
	CFileException e;
	CString Input;
	do
	{
		if (IDOK != Import.DoModal())
			return FALSE;

		ImportFile = Import.GetFileName();
	}
	while (!f.Open( ImportFile, CFile::modeRead, &e ));

	CArchive i(&f,CArchive::load);

	while(i.ReadString(Input))
	{
		TRACE("%s\n",Input);
	}
	return FALSE;
}




Mike Blaszczak -- mikeblas@msn.com
Friday, January 26, 1996

Andrew Alexander wrote:

> CArchive::ReadString(CString& rString)  appears to be flawed.

This has been fixed for MFC 4.1.  You'll note that CStdioFile::ReadString() 
has a similar problem.

.B ekiM
TCHAR szOhMan[] = _T("Thiss werds nahmmyon, I'ff haddladda vodka.")




| Вернуться в корень Архива |