diff --git a/docs/changes.txt b/docs/changes.txt index e362ebcb4c..0f60cca2d5 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -73,6 +73,7 @@ All: - Make it easier to convert to/from UTF-8-encoded std::string (ARATA Mizuki). - Add support for loading dynamic lexer in wxStyledTextCtrl (New Pagodi). - Handle strings with embedded NULs in wxDataStream (Nitch). +- Don't crash in wxTextFile::GetLastLine() if the file is empty (crohr). All (GUI): diff --git a/include/wx/textbuf.h b/include/wx/textbuf.h index a2511b427d..0400fa4ee7 100644 --- a/include/wx/textbuf.h +++ b/include/wx/textbuf.h @@ -125,7 +125,7 @@ public: wxString& GetPrevLine() /* const */ { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; } wxString& GetLastLine() /* const */ - { m_nCurLine = m_aLines.size() - 1; return m_aLines.Last(); } + { return m_aLines.empty() ? ms_eof : m_aLines[m_nCurLine = m_aLines.size() - 1]; } // get the type of the line (see also GetEOL) wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; } diff --git a/tests/textfile/textfiletest.cpp b/tests/textfile/textfiletest.cpp index 7fa475c54c..87bbdd5a20 100644 --- a/tests/textfile/textfiletest.cpp +++ b/tests/textfile/textfiletest.cpp @@ -117,6 +117,9 @@ void TextFileTestCase::ReadEmpty() CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); CPPUNIT_ASSERT_EQUAL( (size_t)0, f.GetLineCount() ); + CPPUNIT_ASSERT( f.Eof() ); + CPPUNIT_ASSERT_EQUAL( "", f.GetFirstLine() ); + CPPUNIT_ASSERT_EQUAL( "", f.GetLastLine() ); } void TextFileTestCase::ReadDOS()