[commit] r1329 - in trunk/GME: Common Console Gme MgaUtil ObjectInspector

GMESRC Repository Notifications gme-commit at list.isis.vanderbilt.edu
Tue May 31 11:23:31 CDT 2011


Author: ksmyth
Date: Tue May 31 11:23:31 2011
New Revision: 1329

Log:
Encapsulate CEdit::GetLine, which is actually very hard to get right. Notably not null-terminated in GmeDlg.cpp and ModelPropertiesDlgBar.cpp.

Modified:
   trunk/GME/Common/CommonMfc.h
   trunk/GME/Console/ScriptEdit.cpp
   trunk/GME/Gme/ModelPropertiesDlgBar.cpp
   trunk/GME/MgaUtil/GmeDlg.cpp
   trunk/GME/ObjectInspector/InPlaceManager.cpp

Modified: trunk/GME/Common/CommonMfc.h
==============================================================================
--- trunk/GME/Common/CommonMfc.h	Tue May 31 09:39:08 2011	(r1328)
+++ trunk/GME/Common/CommonMfc.h	Tue May 31 11:23:31 2011	(r1329)
@@ -225,6 +225,21 @@
 	CopyTo(a, p);
 }
 
+static CString CEditGetLine(const CEdit& edit, int line=-1) {
+	int charsInLine = edit.LineLength(edit.LineIndex(line));
+	// n.b. If the edit control is empty, GetLine call returns two null terminators
+	if (charsInLine == 0)
+		return _T("");
+	// n.b. CEdit::GetLine sends EM_GETLINE, so it must set the first word to the buffer size
+	int charsInBuffer = max(sizeof(void*), charsInLine+1);
+	CString ret;
+	// n.b. the CEdit::GetLine MSDN documentation is wrong: GetLine returns the number of characters (not bytes)
+	// CEdit::GetLine returns (int)::SendMessage(...EM_GETLINE...) and the EM_GETLINE docs specify characters
+	VERIFY(edit.GetLine(line, ret.GetBuffer(charsInBuffer), charsInBuffer) == charsInLine);
+	ret.ReleaseBuffer(charsInLine);
+	return ret;
+}
+
 // --------------------------- Error
 
 void DisplayError(const TCHAR *msg, HRESULT hr) NOTHROW;

Modified: trunk/GME/Console/ScriptEdit.cpp
==============================================================================
--- trunk/GME/Console/ScriptEdit.cpp	Tue May 31 09:39:08 2011	(r1328)
+++ trunk/GME/Console/ScriptEdit.cpp	Tue May 31 11:23:31 2011	(r1329)
@@ -37,7 +37,7 @@
 		COMTHROW(m_host.CreateInstance(CLSID_ScriptHost));
 		_bstr_t engine(L"JScript");
 		COMTHROW(m_host->InitEngine((void*)m_console, engine));
-		this->LimitText( 256);     // modify in accordance with GetLine( ..., 256) in OnKeyUp()
+		this->LimitText( 256);
 		this->SetWindowText( defPrompt); // to attract user attention
 	}
 	catch(hresult_exception &e) 
@@ -101,11 +101,7 @@
 	else if (nChar == VK_RETURN)
 	{
 		lastup = -1;
-		CString inp;
-
-		int nCopied = GetLine(0, inp.GetBuffer(256), 256);
-		inp.ReleaseBuffer( nCopied); // specify how long the string is supposed to be
-		ASSERT( nCopied == inp.GetLength());
+		CString inp = CEditGetLine(*this, 0);
 
 		_bstr_t binp = inp;
 		// echo

Modified: trunk/GME/Gme/ModelPropertiesDlgBar.cpp
==============================================================================
--- trunk/GME/Gme/ModelPropertiesDlgBar.cpp	Tue May 31 09:39:08 2011	(r1328)
+++ trunk/GME/Gme/ModelPropertiesDlgBar.cpp	Tue May 31 11:23:31 2011	(r1329)
@@ -73,8 +73,7 @@
 		return;
 	if (fwin->m_hWnd == edit->m_hWnd)
 	{
-		TCHAR buff[100];
-		edit->GetLine(0, buff, sizeof(buff)/sizeof(buff[0])-1);
+		CString buff = CEditGetLine(*edit, 0);
 		int kk = _ttoi(buff);
 		kk = abs(kk);
 		if (!kk)
@@ -159,8 +158,7 @@
 	CEdit* edit = (CEdit*)(zoom->GetDlgItem(IDC_TYPENAME));
 	if (!edit)
 		return;
-	TCHAR buff[100];
-	edit->GetLine(0, buff, sizeof(buff)/sizeof(buff[0])-1);
+	CString buff = CEditGetLine(*edit, 0);
 	kk = _ttoi(buff);
 	if (!kk)
 		kk = 100;
@@ -182,8 +180,7 @@
 	CEdit* edit = (CEdit*)(zoom->GetDlgItem(IDC_TYPENAME));
 	if (!edit)
 		return;
-	TCHAR buff[100];
-	edit->GetLine(0, buff, sizeof(buff)/sizeof(buff[0])-1);
+	CString buff = CEditGetLine(*edit, 0);
 	kk = _ttoi(buff);
 	if (!kk)
 		kk = 100;

Modified: trunk/GME/MgaUtil/GmeDlg.cpp
==============================================================================
--- trunk/GME/MgaUtil/GmeDlg.cpp	Tue May 31 09:39:08 2011	(r1328)
+++ trunk/GME/MgaUtil/GmeDlg.cpp	Tue May 31 11:23:31 2011	(r1329)
@@ -642,8 +642,7 @@
 		edit = (CEdit*)(zoom->GetDlgItem(1001));
 		if (edit)
 		{
-			TCHAR buff[100];
-			edit->GetLine(0, buff, sizeof(buff)-1);
+			CString buff = CEditGetLine(*edit, 0);
 
 			int zv = 0;
 			/* for now accept only positive numbers, commented out, see COMMENT above
@@ -669,7 +668,8 @@
 			if( l != 1 || zv < ZOOM_MIN || zv > ZOOM_MAX)
 				zv = 100;
 
-			_itot(zv, buff, 10);
+			_itot(zv, buff.GetBuffer(35), 10);
+			buff.ReleaseBuffer();
 			res = buff;
 		}
 	}
@@ -734,9 +734,7 @@
 		edit = (CEdit*)( combo->GetDlgItem(1001));
 		if( edit)
 		{
-			TCHAR buff[100];
-			edit->GetLine(0, buff, sizeof(buff));
-			res = buff;
+			res = CEditGetLine(*edit, 0);
 		}
 	}
 	return res;
@@ -776,12 +774,11 @@
 	{
 		CEdit* edit = 0;
 		edit = (CEdit*)( combo->GetDlgItem(1001));
-		if( edit)
+		if (edit)
 		{
-			TCHAR buff[100];
-			edit->GetLine(0, buff, sizeof(buff));
-			int value = _ttoi( buff);
-			if( value > 0 && value < 100) // above 0 and below 100
+			CString buff = CEditGetLine(*edit, 0);
+			int value = _ttoi(buff);
+			if (value > 0 && value < 100) // above 0 and below 100
 				res = buff;
 		}
 	}
@@ -828,9 +825,7 @@
 		edit = (CEdit*)(combo->GetDlgItem(1001));
 		if (edit)
 		{
-			TCHAR buff[100];
-			edit->GetLine(0, buff, sizeof(buff));
-			res = buff;
+			res = CEditGetLine(*edit, 0);
 		}
 	}
 	edgesmoothmode_enum edgeSmoothMode = EdgeSmooth_HighQualityMode;
@@ -889,9 +884,7 @@
 		edit = (CEdit*)(combo->GetDlgItem(1001));
 		if (edit)
 		{
-			TCHAR buff[100];
-			edit->GetLine(0, buff, sizeof(buff));
-			res = buff;
+			res = CEditGetLine(*edit, 0);
 		}
 	}
 	fontsmoothmode_enum fontSmoothMode = FontSmooth_AntiAlias;

Modified: trunk/GME/ObjectInspector/InPlaceManager.cpp
==============================================================================
--- trunk/GME/ObjectInspector/InPlaceManager.cpp	Tue May 31 09:39:08 2011	(r1328)
+++ trunk/GME/ObjectInspector/InPlaceManager.cpp	Tue May 31 11:23:31 2011	(r1329)
@@ -655,18 +655,9 @@
 	//         (corrupted strLine inserted into ListItem caused memory damage when released)
 	if( nLineLength > 0 )
 	{
-		// We should supply at least two bytes length buffer, GetLine has a bug, see above
-		if (nLineLength <= 1)
-			nLineLength = 2;
 		for(int i=0;i<nLines;i++)
 		{
-			strLine=_T("");
-			// nLineLength=m_MultiEditCtrl.LineLength(i);
-			// LineLength can have a bug!
-			// Note, that everything works in ANSI here, Unicode would need changes here
-			int nRet=m_MultiEditCtrl.GetLine(i,strLine.GetBuffer(nLineLength), nLineLength);
-			ASSERT( nRet >= 0);
-			strLine.ReleaseBuffer(nRet);
+			strLine = CEditGetLine(m_MultiEditCtrl, i);
 			strLine.TrimRight(_T("\r\n"));
 			ListItem.Value.stringVal.Add(strLine);
 		}


More information about the gme-commit mailing list