Skip to content

Commit 24e3b52

Browse files
committed
Fixed (?) bug where notepad windows might load garbage
1 parent 0665664 commit 24e3b52

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

TextDocument.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void CTextDocument::Dump(CDumpContext& dc) const
115115
void CTextDocument::Serialize(CArchive& ar)
116116
{
117117
// CEditView contains an edit control which handles all serialization
118-
((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
118+
((CTextView*)m_viewList.GetHead())->SerializeRaw(ar);
119119

120120
if (ar.IsLoading ())
121121
CreateMonitoringThread (ar.GetFile ()->GetFilePath ());

TextView.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,42 @@ void CTextView::SerializeRaw(CArchive& ar)
238238
{
239239
CFile* pFile = ar.GetFile();
240240
ASSERT(pFile->GetPosition() == 0);
241-
DWORD nFileSize = pFile->GetLength();
241+
DWORD nLen = pFile->GetLength();
242242
// ReadFromArchive takes the number of characters as argument
243-
ReadFromArchive(ar, (UINT)nFileSize/sizeof(TCHAR));
243+
244+
// REPLACING: ReadFromArchive(ar, (UINT)nFileSize/sizeof(TCHAR));
245+
246+
// with this (we got garbage at times):
247+
248+
LPVOID hText = LocalAlloc(LMEM_MOVEABLE, (nLen+1)*sizeof(TCHAR));
249+
if (hText == NULL)
250+
AfxThrowMemoryException();
251+
252+
LPTSTR lpszText = (LPTSTR)LocalLock(hText);
253+
ASSERT(lpszText != NULL);
254+
if (ar.Read(lpszText, nLen*sizeof(TCHAR)) != nLen*sizeof(TCHAR))
255+
{
256+
LocalUnlock(hText);
257+
LocalFree(hText);
258+
AfxThrowArchiveException(CArchiveException::endOfFile);
259+
}
260+
// Replace the editing edit buffer with the newly loaded data
261+
lpszText[nLen] = '\0';
262+
263+
// set the text with SetWindowText, then free
264+
BOOL bResult = ::SetWindowText(m_hWnd, lpszText);
265+
LocalUnlock(hText);
266+
LocalFree(hText);
267+
268+
// make sure that SetWindowText was successful
269+
if (!bResult || ::GetWindowTextLength(m_hWnd) < (int)nLen)
270+
AfxThrowMemoryException();
271+
272+
// remove old shadow buffer
273+
delete[] m_pShadowBuffer;
274+
m_pShadowBuffer = NULL;
275+
m_nShadowSize = 0;
276+
244277
}
245278
ASSERT_VALID(this);
246279
} /* end of CTextView::SerializeRaw */

0 commit comments

Comments
 (0)