X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/29a96d3c098cefe7cb388ee914b8e496d7970406..55129143a164e44f832c7438c79310ebe26675d5:/src/common/textfile.cpp diff --git a/src/common/textfile.cpp b/src/common/textfile.cpp index db0d4b41ba..91a86d5fb4 100644 --- a/src/common/textfile.cpp +++ b/src/common/textfile.cpp @@ -64,7 +64,7 @@ bool wxTextFile::OnOpen(const wxString &strBufferName, wxTextBufferOpenMode Open switch ( OpenMode ) { default: - wxFAIL_MSG( _T("unknown open mode in wxTextFile::Open") ); + wxFAIL_MSG( wxT("unknown open mode in wxTextFile::Open") ); // fall through case ReadAccess : @@ -89,7 +89,7 @@ bool wxTextFile::OnClose() bool wxTextFile::OnRead(const wxMBConv& conv) { // file should be opened - wxASSERT_MSG( m_file.IsOpened(), _T("can't read closed file") ); + wxASSERT_MSG( m_file.IsOpened(), wxT("can't read closed file") ); // read the entire file in memory: this is not the most efficient thing to // do it but there is no good way to avoid it in Unicode build because if @@ -124,12 +124,19 @@ bool wxTextFile::OnRead(const wxMBConv& conv) return false; // if the file is seekable, also check that we're at its beginning - wxASSERT_MSG( m_file.Tell() == 0, _T("should be at start of file") ); + wxASSERT_MSG( m_file.Tell() == 0, wxT("should be at start of file") ); char *dst = buf.data(); - for ( ;; ) + for ( size_t nRemaining = bufSize; nRemaining > 0; ) { - ssize_t nRead = m_file.Read(dst, BLOCK_SIZE); + size_t nToRead = BLOCK_SIZE; + + // the file size could have changed, avoid overflowing the buffer + // even if it did + if ( nToRead > nRemaining ) + nToRead = nRemaining; + + ssize_t nRead = m_file.Read(dst, nToRead); if ( nRead == wxInvalidOffset ) { @@ -140,14 +147,16 @@ bool wxTextFile::OnRead(const wxMBConv& conv) if ( nRead == 0 ) { // this file can't be empty because we checked for this above + // so this must be the end of file break; } dst += nRead; + nRemaining -= nRead; } wxASSERT_MSG( dst - buf.data() == (wxFileOffset)bufSize, - _T("logic error") ); + wxT("logic error") ); } else // file is not seekable {